qt: fixed great ui pains with tunnels editing

This commit is contained in:
user 2020-12-18 05:37:01 +08:00
parent 9c6e3ff1d7
commit 669fb62a54
10 changed files with 68 additions and 53 deletions

View File

@ -2,6 +2,7 @@
#define DELAYEDSAVEMANAGER_H #define DELAYEDSAVEMANAGER_H
#include "Saver.h" #include "Saver.h"
#include "I2pdQtTypes.h"
class DelayedSaveManager class DelayedSaveManager
{ {
@ -12,13 +13,14 @@ public:
typedef unsigned int DATA_SERIAL_TYPE; typedef unsigned int DATA_SERIAL_TYPE;
virtual void delayedSave(DATA_SERIAL_TYPE dataSerial, bool needsTunnelFocus, std::string tunnelNameToFocus)=0; virtual void delayedSave(bool reloadAfterSave, DATA_SERIAL_TYPE dataSerial, FocusEnum focusOn, std::string tunnelNameToFocus, QWidget* widgetToFocus)=0;
//returns false iff save failed //returns false iff save failed
virtual bool appExiting()=0; virtual bool appExiting()=0;
virtual bool needsFocusOnTunnel()=0; virtual FocusEnum getFocusOn()=0;
virtual std::string& getTunnelNameToFocus()=0; virtual std::string& getTunnelNameToFocus()=0;
virtual QWidget* getWidgetToFocus()=0;
}; };
#endif // DELAYEDSAVEMANAGER_H #endif // DELAYEDSAVEMANAGER_H

View File

@ -1,6 +1,7 @@
#include "DelayedSaveManagerImpl.h" #include "DelayedSaveManagerImpl.h"
DelayedSaveManagerImpl::DelayedSaveManagerImpl() : DelayedSaveManagerImpl::DelayedSaveManagerImpl() :
widgetToFocus(nullptr),
saver(nullptr), saver(nullptr),
lastDataSerialSeen(DelayedSaveManagerImpl::INITIAL_DATA_SERIAL), lastDataSerialSeen(DelayedSaveManagerImpl::INITIAL_DATA_SERIAL),
lastSaveStartedTimestamp(A_VERY_OBSOLETE_TIMESTAMP), lastSaveStartedTimestamp(A_VERY_OBSOLETE_TIMESTAMP),
@ -21,10 +22,12 @@ bool DelayedSaveManagerImpl::isSaverValid() {
return saver != nullptr; return saver != nullptr;
} }
void DelayedSaveManagerImpl::delayedSave(DATA_SERIAL_TYPE dataSerial, bool focusOnTunnel, std::string tunnelNameToFocus_) { void DelayedSaveManagerImpl::delayedSave(bool reloadAfterSave, DATA_SERIAL_TYPE dataSerial, FocusEnum focusOn, std::string tunnelNameToFocus, QWidget* widgetToFocus) {
if(lastDataSerialSeen==dataSerial)return; if(lastDataSerialSeen==dataSerial)return;
this->focusOnTunnel = focusOnTunnel; this->reloadAfterSave = reloadAfterSave;
tunnelNameToFocus = tunnelNameToFocus_; this->focusOn = focusOn;
this->tunnelNameToFocus = tunnelNameToFocus;
this->widgetToFocus = widgetToFocus;
lastDataSerialSeen=dataSerial; lastDataSerialSeen=dataSerial;
assert(isSaverValid()); assert(isSaverValid());
TIMESTAMP_TYPE now = getTime(); TIMESTAMP_TYPE now = getTime();
@ -42,7 +45,7 @@ bool DelayedSaveManagerImpl::appExiting() {
exiting=true; exiting=true;
thread->wakeThreadAndJoinThread(); thread->wakeThreadAndJoinThread();
assert(isSaverValid()); assert(isSaverValid());
saver->save(false, ""); saver->save(false, FocusEnum::noFocus);
return true; return true;
} }
@ -71,9 +74,10 @@ void DelayedSaveThread::run() {
assert(saver!=nullptr); assert(saver!=nullptr);
if(saveNow) { if(saveNow) {
saveNow = false; saveNow = false;
const bool focusOnTunnel = delayedSaveManagerImpl->needsFocusOnTunnel(); const FocusEnum focusOn = delayedSaveManagerImpl->getFocusOn();
const std::string tunnelNameToFocus = delayedSaveManagerImpl->getTunnelNameToFocus(); const std::string tunnelNameToFocus = delayedSaveManagerImpl->getTunnelNameToFocus();
saver->save(focusOnTunnel, tunnelNameToFocus); QWidget* widgetToFocus = delayedSaveManagerImpl->getWidgetToFocus();
saver->save(delayedSaveManagerImpl->isReloadAfterSave(), focusOn, tunnelNameToFocus, widgetToFocus);
continue; continue;
} }
if(defer) { if(defer) {
@ -87,9 +91,10 @@ void DelayedSaveThread::run() {
if(delayedSaveManagerImpl->isExiting())return; if(delayedSaveManagerImpl->isExiting())return;
continue; continue;
} }
const bool focusOnTunnel = delayedSaveManagerImpl->needsFocusOnTunnel(); const FocusEnum focusOn = delayedSaveManagerImpl->getFocusOn();
const std::string tunnelNameToFocus = delayedSaveManagerImpl->getTunnelNameToFocus(); const std::string tunnelNameToFocus = delayedSaveManagerImpl->getTunnelNameToFocus();
saver->save(focusOnTunnel, tunnelNameToFocus); QWidget* widgetToFocus = delayedSaveManagerImpl->getWidgetToFocus();
saver->save(delayedSaveManagerImpl->isReloadAfterSave(), focusOn, tunnelNameToFocus, widgetToFocus);
break; //break inner loop break; //break inner loop
} }
} }
@ -131,10 +136,3 @@ Saver* DelayedSaveManagerImpl::getSaver() {
return saver; return saver;
} }
bool DelayedSaveManagerImpl::needsFocusOnTunnel() {
return focusOnTunnel;
}
std::string& DelayedSaveManagerImpl::getTunnelNameToFocus() {
return tunnelNameToFocus;
}

View File

@ -7,14 +7,14 @@
#include <QMutex> #include <QMutex>
#include <QDateTime> #include <QDateTime>
#include "mainwindow.h" #include "I2pdQtTypes.h"
#include "DelayedSaveManager.h" #include "DelayedSaveManager.h"
#include "Saver.h" #include "Saver.h"
class DelayedSaveManagerImpl; class DelayedSaveManagerImpl;
class Saver;
class DelayedSaveThread : public QThread class DelayedSaveThread : public QThread {
{
Q_OBJECT Q_OBJECT
public: public:
@ -42,14 +42,17 @@ private:
volatile TIMESTAMP_TYPE wakeTime; volatile TIMESTAMP_TYPE wakeTime;
}; };
class DelayedSaveManagerImpl : public DelayedSaveManager class DelayedSaveManagerImpl : public DelayedSaveManager {
{ FocusEnum focusOn;
std::string tunnelNameToFocus;
QWidget* widgetToFocus;
bool reloadAfterSave;
public: public:
DelayedSaveManagerImpl(); DelayedSaveManagerImpl();
virtual ~DelayedSaveManagerImpl(); virtual ~DelayedSaveManagerImpl();
virtual void setSaver(Saver* saver); virtual void setSaver(Saver* saver);
virtual void start(); virtual void start();
virtual void delayedSave(DATA_SERIAL_TYPE dataSerial, bool focusOnTunnel, std::string tunnelNameToFocus); virtual void delayedSave(bool reloadAfterSave, DATA_SERIAL_TYPE dataSerial, FocusEnum focusOn, std::string tunnelNameToFocus, QWidget* widgetToFocus);
virtual bool appExiting(); virtual bool appExiting();
typedef DelayedSaveThread::TIMESTAMP_TYPE TIMESTAMP_TYPE; typedef DelayedSaveThread::TIMESTAMP_TYPE TIMESTAMP_TYPE;
@ -59,8 +62,10 @@ public:
Saver* getSaver(); Saver* getSaver();
static TIMESTAMP_TYPE getTime(); static TIMESTAMP_TYPE getTime();
bool needsFocusOnTunnel(); bool isReloadAfterSave() { return reloadAfterSave; }
std::string& getTunnelNameToFocus(); FocusEnum getFocusOn() { return focusOn; }
std::string& getTunnelNameToFocus() { return tunnelNameToFocus; }
QWidget* getWidgetToFocus() { return widgetToFocus; }
private: private:
Saver* saver; Saver* saver;
@ -74,9 +79,6 @@ private:
bool exiting; bool exiting;
DelayedSaveThread* thread; DelayedSaveThread* thread;
void wakeThreadAndJoinThread(); void wakeThreadAndJoinThread();
bool focusOnTunnel;
std::string tunnelNameToFocus;
}; };
#endif // DELAYEDSAVEMANAGERIMPL_H #endif // DELAYEDSAVEMANAGERIMPL_H

7
qt/i2pd_qt/I2pdQtTypes.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef I2PDQTTYPES_H
#define I2PDQTTYPES_H
enum WrongInputPageEnum { generalSettingsPage, tunnelsSettingsPage };
enum FocusEnum { noFocus, focusOnTunnelName, focusOnWidget };
#endif // I2PDQTTYPES_H

View File

@ -4,6 +4,9 @@
#include <string> #include <string>
#include <QObject> #include <QObject>
#include <QString> #include <QString>
class QWidget;
#include "I2pdQtTypes.h"
class Saver : public QObject class Saver : public QObject
{ {
@ -11,8 +14,8 @@ class Saver : public QObject
public: public:
Saver(); Saver();
//false iff failures //FocusEnum::focusNone iff failures //??? wtf
virtual bool save(const bool focusOnTunnel, const std::string& tunnelNameToFocus)=0; virtual bool save(bool reloadAfterSave, const FocusEnum focusOn, const std::string& tunnelNameToFocus="", QWidget* widgetToFocus=nullptr)=0;
signals: signals:
void reloadTunnelsConfigAndUISignal(const QString); void reloadTunnelsConfigAndUISignal(const QString);

View File

@ -15,7 +15,7 @@ SaverImpl::SaverImpl(MainWindow *mainWindowPtr_, QList<MainWindowItem*> * config
SaverImpl::~SaverImpl() {} SaverImpl::~SaverImpl() {}
bool SaverImpl::save(const bool focusOnTunnel, const std::string& tunnelNameToFocus) { bool SaverImpl::save(bool reloadAfterSave, const FocusEnum focusOn, const std::string& tunnelNameToFocus, QWidget* widgetToFocus) {
//save main config //save main config
{ {
std::stringstream out; std::stringstream out;
@ -59,12 +59,14 @@ bool SaverImpl::save(const bool focusOnTunnel, const std::string& tunnelNameToFo
outfile.close(); outfile.close();
} }
if(reloadAfterSave) {
//reload saved configs //reload saved configs
#if 0 #if 0
i2p::client::context.ReloadConfig(); i2p::client::context.ReloadConfig();
#endif #endif
if(focusOnTunnel) emit reloadTunnelsConfigAndUISignal(QString::fromStdString(tunnelNameToFocus)); if(reloadAfterSave) emit reloadTunnelsConfigAndUISignal(focusOn==FocusEnum::focusOnTunnelName?QString::fromStdString(tunnelNameToFocus):"");
}
return true; return true;
} }

View File

@ -19,7 +19,7 @@ class SaverImpl : public Saver
public: public:
SaverImpl(MainWindow *mainWindowPtr_, QList<MainWindowItem*> * configItems_, std::map<std::string,TunnelConfig*>* tunnelConfigs_); SaverImpl(MainWindow *mainWindowPtr_, QList<MainWindowItem*> * configItems_, std::map<std::string,TunnelConfig*>* tunnelConfigs_);
virtual ~SaverImpl(); virtual ~SaverImpl();
virtual bool save(const bool focusOnTunnel, const std::string& tunnelNameToFocus); virtual bool save(bool reloadAfterSave, const FocusEnum focusOn, const std::string& tunnelNameToFocus, QWidget* widgetToFocus);
void setConfPath(QString& confpath_); void setConfPath(QString& confpath_);
void setTunnelsConfPath(QString& tunconfpath_); void setTunnelsConfPath(QString& tunconfpath_);
private: private:

View File

@ -64,7 +64,8 @@ HEADERS += DaemonQT.h mainwindow.h \
../../daemon/UPnP.h \ ../../daemon/UPnP.h \
AboutDialog.h \ AboutDialog.h \
BuildDateTimeQt.h \ BuildDateTimeQt.h \
I2pdQtUtil.h I2pdQtUtil.h \
I2pdQtTypes.h
INCLUDEPATH += ../../libi2pd INCLUDEPATH += ../../libi2pd
INCLUDEPATH += ../../libi2pd_client INCLUDEPATH += ../../libi2pd_client

View File

@ -783,7 +783,7 @@ void MainWindow::deleteTunnelFromUI(std::string tunnelName, TunnelConfig* cnf) {
} }
/** returns false iff not valid items present and save was aborted */ /** returns false iff not valid items present and save was aborted */
bool MainWindow::saveAllConfigs(bool focusOnTunnel, std::string tunnelNameToFocus){ bool MainWindow::saveAllConfigs(bool reloadAfterSave, FocusEnum focusOn, std::string tunnelNameToFocus, QWidget* widgetToFocus){
QString cannotSaveSettings = QApplication::tr("Cannot save settings."); QString cannotSaveSettings = QApplication::tr("Cannot save settings.");
programOptionsWriterCurrentSection=""; programOptionsWriterCurrentSection="";
/*if(!logFileNameOption->lineEdit->text().trimmed().isEmpty())logOption->optionValue=boost::any(std::string("file")); /*if(!logFileNameOption->lineEdit->text().trimmed().isEmpty())logOption->optionValue=boost::any(std::string("file"));
@ -803,7 +803,7 @@ bool MainWindow::saveAllConfigs(bool focusOnTunnel, std::string tunnelNameToFocu
return false; return false;
} }
} }
delayedSaveManagerPtr->delayedSave(++dataSerial, focusOnTunnel, tunnelNameToFocus);//TODO does dataSerial work? //FIXME delayedSaveManagerPtr->delayedSave(reloadAfterSave, ++dataSerial, focusOn, tunnelNameToFocus, widgetToFocus);//TODO does dataSerial work? //FIXME
//onLoggingOptionsChange(); //onLoggingOptionsChange();
return true; return true;
@ -841,7 +841,7 @@ void MainWindow::updated() {
bool correct = applyTunnelsUiToConfigs(); bool correct = applyTunnelsUiToConfigs();
if(!correct) return; if(!correct) return;
saveAllConfigs(false); saveAllConfigs(false, FocusEnum::noFocus);
} }
void MainWindowItem::installListeners(MainWindow *mainWindow) {} void MainWindowItem::installListeners(MainWindow *mainWindow) {}
@ -916,11 +916,11 @@ bool MainWindow::applyTunnelsUiToConfigs() {
return true; return true;
} }
void MainWindow::reloadTunnelsConfigAndUI_QString(const QString tunnelNameToFocus) { void MainWindow::reloadTunnelsConfigAndUI_QString(QString tunnelNameToFocus) {
reloadTunnelsConfigAndUI(tunnelNameToFocus.toStdString()); reloadTunnelsConfigAndUI(tunnelNameToFocus.toStdString(), nullptr);
} }
void MainWindow::reloadTunnelsConfigAndUI(std::string tunnelNameToFocus) { void MainWindow::reloadTunnelsConfigAndUI(std::string tunnelNameToFocus, QWidget* widgetToFocus) {
deleteTunnelForms(); deleteTunnelForms();
for (std::map<std::string,TunnelConfig*>::iterator it=tunnelConfigs.begin(); it!=tunnelConfigs.end(); ++it) { for (std::map<std::string,TunnelConfig*>::iterator it=tunnelConfigs.begin(); it!=tunnelConfigs.end(); ++it) {
TunnelConfig* tunconf = it->second; TunnelConfig* tunconf = it->second;
@ -937,8 +937,10 @@ void MainWindow::TunnelsPageUpdateListenerMainWindowImpl::updated(std::string ol
std::map<std::string,TunnelConfig*>::const_iterator it=mainWindow->tunnelConfigs.find(oldName); std::map<std::string,TunnelConfig*>::const_iterator it=mainWindow->tunnelConfigs.find(oldName);
if(it!=mainWindow->tunnelConfigs.end())mainWindow->tunnelConfigs.erase(it); if(it!=mainWindow->tunnelConfigs.end())mainWindow->tunnelConfigs.erase(it);
mainWindow->tunnelConfigs[tunConf->getName()]=tunConf; mainWindow->tunnelConfigs[tunConf->getName()]=tunConf;
mainWindow->saveAllConfigs(true, FocusEnum::focusOnTunnelName, tunConf->getName());
} }
mainWindow->saveAllConfigs(true, tunConf->getName()); else
mainWindow->saveAllConfigs(false, FocusEnum::noFocus);
} }
void MainWindow::TunnelsPageUpdateListenerMainWindowImpl::needsDeleting(std::string oldName){ void MainWindow::TunnelsPageUpdateListenerMainWindowImpl::needsDeleting(std::string oldName){

View File

@ -1,8 +1,6 @@
#ifndef MAINWINDOW_H #ifndef MAINWINDOW_H
#define MAINWINDOW_H #define MAINWINDOW_H
enum WrongInputPageEnum { generalSettingsPage, tunnelsSettingsPage };
#include <QObject> #include <QObject>
#include <QMainWindow> #include <QMainWindow>
#include <QPushButton> #include <QPushButton>
@ -136,7 +134,7 @@ public:
std::string optName=""; std::string optName="";
if(!option.section.isEmpty())optName=option.section.toStdString()+std::string("."); if(!option.section.isEmpty())optName=option.section.toStdString()+std::string(".");
optName+=option.option.toStdString(); optName+=option.option.toStdString();
qDebug() << "Writing option" << optName.c_str() << "of type" << rtti.c_str(); //qDebug() << "Writing option" << optName.c_str() << "of type" << rtti.c_str();
std::string sectionAsStdStr = option.section.toStdString(); std::string sectionAsStdStr = option.section.toStdString();
if(!option.section.isEmpty() && if(!option.section.isEmpty() &&
sectionAsStdStr!=programOptionsWriterCurrentSection) { sectionAsStdStr!=programOptionsWriterCurrentSection) {
@ -541,12 +539,12 @@ protected:
public slots: public slots:
/** returns false iff not valid items present and save was aborted */ /** returns false iff not valid items present and save was aborted */
bool saveAllConfigs(bool focusOnTunnel, std::string tunnelNameToFocus=""); bool saveAllConfigs(bool reloadAfterSave, FocusEnum focusOn, std::string tunnelNameToFocus="", QWidget* widgetToFocus=nullptr);
void reloadTunnelsConfigAndUI(std::string tunnelNameToFocus); void reloadTunnelsConfigAndUI(std::string tunnelNameToFocus, QWidget* widgetToFocus);
void reloadTunnelsConfigAndUI() { reloadTunnelsConfigAndUI("", nullptr); }
//focus none //focus none
void reloadTunnelsConfigAndUI() { reloadTunnelsConfigAndUI(""); } void reloadTunnelsConfigAndUI_QString(QString tunnelNameToFocus);
void reloadTunnelsConfigAndUI_QString(const QString tunnelNameToFocus);
void addServerTunnelPushButtonReleased(); void addServerTunnelPushButtonReleased();
void addClientTunnelPushButtonReleased(); void addClientTunnelPushButtonReleased();
@ -651,7 +649,7 @@ private:
tunnelConfigs.erase(it); tunnelConfigs.erase(it);
delete tc; delete tc;
} }
saveAllConfigs(false); saveAllConfigs(true, FocusEnum::noFocus);
} }
std::string GenerateNewTunnelName() { std::string GenerateNewTunnelName() {
@ -688,7 +686,7 @@ private:
sigType, sigType,
cryptoType); cryptoType);
saveAllConfigs(true, name); saveAllConfigs(true, FocusEnum::focusOnTunnelName, name);
} }
void CreateDefaultServerTunnel() {//TODO dedup default values with ReadTunnelsConfig() and with ClientContext.cpp::ReadTunnels () void CreateDefaultServerTunnel() {//TODO dedup default values with ReadTunnelsConfig() and with ClientContext.cpp::ReadTunnels ()
@ -726,7 +724,7 @@ private:
cryptoType); cryptoType);
saveAllConfigs(true, name); saveAllConfigs(true, FocusEnum::focusOnTunnelName, name);
} }
void ReadTunnelsConfig() //TODO deduplicate the code with ClientContext.cpp::ReadTunnels () void ReadTunnelsConfig() //TODO deduplicate the code with ClientContext.cpp::ReadTunnels ()