diff options
-rw-r--r-- | lib/configuration/CMakeLists.txt | 10 | ||||
-rw-r--r-- | lib/configuration/configuration.cpp | 9 | ||||
-rw-r--r-- | lib/configuration/configuration.h | 42 | ||||
-rw-r--r-- | src/browser.cpp | 4 | ||||
-rw-r--r-- | src/main.cpp | 6 | ||||
-rw-r--r-- | src/mainwindow/mainwindow.cpp | 38 |
6 files changed, 82 insertions, 27 deletions
diff --git a/lib/configuration/CMakeLists.txt b/lib/configuration/CMakeLists.txt index 0db2b67..ed0235d 100644 --- a/lib/configuration/CMakeLists.txt +++ b/lib/configuration/CMakeLists.txt @@ -1,3 +1,11 @@ +# Find includes in corresponding build directories +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +# Instruct CMake to run moc automatically when needed. +set(CMAKE_AUTOMOC ON) +#set(CMAKE_AUTOUIC ON) +#set(CMAKE_AUTORCC ON) + add_library(configuration configuration.cpp configuration.h) @@ -6,6 +14,6 @@ target_include_directories(configuration PRIVATE ${Boost_INCLUDE_DIRS}) target_link_libraries(configuration - Qt5::Core + Qt5::Core Qt5::Widgets ${Boost_LIBRARIES} ) diff --git a/lib/configuration/configuration.cpp b/lib/configuration/configuration.cpp index e767c65..0c49a83 100644 --- a/lib/configuration/configuration.cpp +++ b/lib/configuration/configuration.cpp @@ -38,7 +38,8 @@ constexpr const char *defaultSocketPath() #endif } -Configuration::Configuration() +Configuration::Configuration(QObject *parent) + : QObject(parent) { m_homePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString(); @@ -53,6 +54,8 @@ Configuration::Configuration() ("args", po::value<std::vector<std::string>>(), "arguments") ; + arguments_desc.add("args", -1); + configuration_desc.add_options() ("browser.stylesheet", po::value<std::string>()) @@ -119,12 +122,8 @@ Configuration::Configuration() ("downloads.path", po::value<std::string>()->default_value("~/Downloads")) ("downloads.shortcut", po::value<std::string>()->default_value("Ctrl+D")) ; - - arguments_desc.add("args", -1); } -Configuration::~Configuration() = default; - bool Configuration::parse(const std::string &path) { std::ifstream f(path, std::ifstream::in); diff --git a/lib/configuration/configuration.h b/lib/configuration/configuration.h index bb1b271..ec834ae 100644 --- a/lib/configuration/configuration.h +++ b/lib/configuration/configuration.h @@ -9,6 +9,7 @@ #ifndef SMOLBOTE_CONFIGURATION_H #define SMOLBOTE_CONFIGURATION_H +#include <QAction> #include <QString> #include <QStringList> #include <QVariant> @@ -17,11 +18,13 @@ #include <string> #include <vector> -class Configuration +class Configuration : public QObject { + Q_OBJECT + public: - explicit Configuration(); - ~Configuration(); + explicit Configuration(QObject *parent = nullptr); + ~Configuration() = default; bool parse(const std::string &path); bool parse(int argc, char **argv); @@ -31,7 +34,8 @@ public: return configuration_desc.options(); } - bool exists(const char *path) { + bool exists(const char *path) + { return vm.count(path) ? true : false; } @@ -54,11 +58,11 @@ public: } else if constexpr(std::is_same_v<T, std::string>) { - if (vm[path].value().type() == typeid(int)) { + if(vm[path].value().type() == typeid(int)) { return std::optional<std::string>(std::to_string(vm[path].as<int>())); } - if (vm[path].value().type() == typeid(bool)) { + if(vm[path].value().type() == typeid(bool)) { return std::optional<std::string>(vm[path].as<bool>() ? "true" : "false"); } @@ -75,6 +79,29 @@ public: return std::optional<T>(vm[path].as<T>()); } + template <typename T> + void setValue(const char *path, const T &value) + { + if(vm.count(path) == 0) { + qWarning("value(%s) does not exist", path); + } + + vm.at(path).value() = value; + + emit settingChanged(path, value); + } + + void setShortcut(QAction *action, const char *name) const + { + Q_CHECK_PTR(action); + + action->setShortcut(QKeySequence::fromString(value<QString>(name).value())); + connect(this, &Configuration::settingChanged, action, [=](const std::string &path, const QString &value) { + if(path == name) + action->setShortcut(QKeySequence::fromString(value)); + }); + } + QHash<QString, QString> section(const std::string &prefix) const; const boost::program_options::options_description commandlineOptions() const { @@ -85,6 +112,9 @@ public: return configuration_desc; } +signals: + void settingChanged(const std::string &path, const QString &value); + private: boost::program_options::options_description commandLine_desc; boost::program_options::options_description configuration_desc; diff --git a/src/browser.cpp b/src/browser.cpp index e26dbc8..56eed96 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -167,7 +167,7 @@ MainWindow *Browser::createWindow() connect(window->addressBar, &AddressBar::complete, m_bookmarks.get(), &BookmarksWidget::search); auto *bookmarksAction = new QAction(tr("Bookmarks"), window); - bookmarksAction->setShortcut(QKeySequence(QString::fromStdString(m_config->value<std::string>("bookmarks.shortcut").value()))); + m_config->setShortcut(bookmarksAction, "bookmarks.shortcut"); connect(bookmarksAction, &QAction::triggered, window, [this, window]() { bool wasVisible = m_bookmarks->isVisible(); for(MainWindow *w : qAsConst(m_windows)) { @@ -180,7 +180,7 @@ MainWindow *Browser::createWindow() window->addAction(MainWindow::ToolsMenu, bookmarksAction); auto *downloadsAction = new QAction(tr("Downloads"), window); - downloadsAction->setShortcut(QKeySequence(QString::fromStdString(m_config->value<std::string>("downloads.shortcut").value()))); + m_config->setShortcut(downloadsAction, "downloads.shortcut"); connect(downloadsAction, &QAction::triggered, window, [this, window]() { bool wasVisible = m_downloads->isVisible(); for(MainWindow *w : qAsConst(m_windows)) { diff --git a/src/main.cpp b/src/main.cpp index c1060aa..ee22753 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,6 +18,12 @@ int main(int argc, char **argv) { // create and load configuration std::shared_ptr<Configuration> config = std::make_shared<Configuration>(); +#ifdef QT_DEBUG + QObject::connect(config.get(), &Configuration::settingChanged, [](const std::string &path, const QString &value) { + qDebug("!!! setting changed %s=[%s]", path.c_str(), qUtf8Printable(value)); + }); +#endif + if(!config->parse(argc, argv)) { qWarning("Error parsing command line, check --help for usage."); return -1; diff --git a/src/mainwindow/mainwindow.cpp b/src/mainwindow/mainwindow.cpp index e885513..27279c1 100644 --- a/src/mainwindow/mainwindow.cpp +++ b/src/mainwindow/mainwindow.cpp @@ -113,8 +113,9 @@ MainWindow::MainWindow(std::shared_ptr<Configuration> &config, QWidget *parent) }); // search box - auto *searchShortcut = new QShortcut(QKeySequence(config->value<std::string>("mainwindow.shortcuts.search").value().c_str()), this); - connect(searchShortcut, &QShortcut::activated, this, [=]() { + auto *searchAction = new QAction(this); + m_config->setShortcut(searchAction, "mainwindow.shortcuts.search"); + connect(searchAction, &QAction::triggered, this, [=]() { /* QTBUG-18665 * When focusing out of the search box and hiding it, the first * (or earlier?) subwindow gets activated for some reason. @@ -127,6 +128,7 @@ MainWindow::MainWindow(std::shared_ptr<Configuration> &config, QWidget *parent) searchBox->show(); } }); + QMainWindow::addAction(searchAction); } MainWindow::~MainWindow() @@ -142,34 +144,44 @@ void MainWindow::createMenuBar() { Q_CHECK_PTR(mdiArea); + // smolbote menu auto *smolboteMenu = menuBar()->addMenu(qApp->applicationDisplayName()); - smolboteMenu->addAction(tr("New subwindow"), this, [this]() { + + auto *subwindowAction = smolboteMenu->addAction(tr("New subwindow"), this, [this]() { createSubWindow(); - }, - QKeySequence(m_config->value<std::string>("mainwindow.shortcuts.newGroup").value().c_str())); + }); + m_config->setShortcut(subwindowAction, "mainwindow.shortcuts.newGroup"); - smolboteMenu->addAction(tr("New window"), this, []() { + auto *windowAction = smolboteMenu->addAction(tr("New window"), this, []() { auto *browser = qobject_cast<Browser *>(qApp); if(browser) browser->createWindow(); - }, - QKeySequence(m_config->value<std::string>("mainwindow.shortcuts.newWindow").value().c_str())); + }); + m_config->setShortcut(windowAction, "mainwindow.shortcuts.newWindow"); smolboteMenu->addSeparator(); - smolboteMenu->addAction(tr("About"), qobject_cast<Browser *>(qApp), &Browser::about, QKeySequence(m_config->value<std::string>("mainwindow.shortcuts.about").value().c_str())); + auto *aboutAction = smolboteMenu->addAction(tr("About"), qobject_cast<Browser *>(qApp), &Browser::about); + m_config->setShortcut(aboutAction, "mainwindow.shortcuts.about"); smolboteMenu->addAction(tr("About Qt"), qApp, &QApplication::aboutQt); smolboteMenu->addSeparator(); - smolboteMenu->addAction(tr("Quit"), qApp, &QApplication::quit, - QKeySequence(m_config->value<std::string>("mainwindow.shortcuts.quit").value().c_str())); + auto *quitAction = smolboteMenu->addAction(tr("Quit"), qApp, &QApplication::quit); + m_config->setShortcut(quitAction, "mainwindow.shortcuts.quit"); + // window menu auto *windowMenu = menuBar()->addMenu(tr("Window")); - windowMenu->addAction(tr("Tile windows"), mdiArea, &QMdiArea::tileSubWindows, QKeySequence(m_config->value<std::string>("mainwindow.shortcuts.tileWindows").value().c_str())); - windowMenu->addAction(tr("Cascade windows"), mdiArea, &QMdiArea::cascadeSubWindows, QKeySequence(m_config->value<std::string>("mainwindow.shortcuts.cascadeWindows").value().c_str())); + + auto *tileAction = windowMenu->addAction(tr("Tile windows"), mdiArea, &QMdiArea::tileSubWindows); + m_config->setShortcut(tileAction, "mainwindow.shortcuts.tileWindows"); + + auto *cascadeAction = windowMenu->addAction(tr("Cascade windows"), mdiArea, &QMdiArea::cascadeSubWindows); + m_config->setShortcut(cascadeAction, "mainwindow.shortcuts.cascadeWindows"); + subWindowAction = windowMenu->addAction(tr("Current window")); + // tools menu toolsMenu = menuBar()->addMenu(tr("Tools")); } |