diff --git a/cmake/QtAppResources.cmake b/cmake/QtAppResources.cmake index 60fb9a9..3cefae5 100644 --- a/cmake/QtAppResources.cmake +++ b/cmake/QtAppResources.cmake @@ -37,7 +37,7 @@ if (UNIX AND NOT APPLE) set(PROJECT_ICON_FILE_NAME "${PROJECT_APPSTREAM_ID}.${PROJECT_ICON_FORMAT}") set(PROJECT_ICON_FILE_PATH "${CMAKE_INSTALL_FULL_DATADIR}/icons/hicolor/scalable/apps") configure_file("resources/icons/application.icon.${PROJECT_ICON_FORMAT}" - "${CMAKE_BINARY_DIR}/${PROJECT_ICON_FILE_NAME}" COPYONLY @ONLY + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_ICON_FILE_NAME}" COPYONLY ) #=========================================================================== # Translations @@ -66,13 +66,13 @@ if (UNIX AND NOT APPLE) #=============================================================================== # Install #=============================================================================== - install(FILES "${PROJECT_APPDATA_FILE_NAME}" + install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_APPDATA_FILE_NAME}" DESTINATION "${CMAKE_INSTALL_DATADIR}/metainfo" ) install(FILES "${PROJECT_DESKTOP_FILES}" DESTINATION "${CMAKE_INSTALL_DATADIR}/applications" ) endif() -install(FILES "${PROJECT_ICON_FILE_NAME}" +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_ICON_FILE_NAME}" DESTINATION "${PROJECT_ICON_FILE_PATH}" ) diff --git a/src/application.cpp b/src/application.cpp index 7996600..fcf5f74 100755 --- a/src/application.cpp +++ b/src/application.cpp @@ -21,21 +21,81 @@ #include #include -#include -QRuler::Application::Application(int &argc, char **argv) +QRuler::Application::Application(int argc, char *argv[]) : QApplication(argc, argv) { setApplicationName(APPLICATION_NAME); setOrganizationName(ORGANIZATION_NAME); setOrganizationDomain(ORGANIZATION_DOMAIN); + initLocale(); + + QString icoCurPath = QCoreApplication::applicationDirPath() + '/' + + QStringLiteral(PROJECT_ICON_NAME); + QString icoSysPath = QStringLiteral(PROJECT_ICON_SYSTEM_PATH); + + // Try first to find the app icon in the current directory + appIcon_ = QIcon(icoCurPath); + if (appIcon_.isNull()) + appIcon_ = QIcon(icoSysPath); + + // UseHighDpiPixmaps is default from Qt6 #if QT_VERSION < 0x060000 setAttribute(Qt::AA_UseHighDpiPixmaps, true); #endif + initUi(); +} + +void QRuler::Application::initLocale() +{ +#if 1 + QLocale locale = QLocale::system(); +#else + QLocale locale(QLocale("it_IT")); + QLocale::setDefault(locale); +#endif + // Qt translations (buttons and the like) + QString translationsPath +#if QT_VERSION < 0x060000 + = QLibraryInfo::location(QLibraryInfo::TranslationsPath); +#else + = QLibraryInfo::path(QLibraryInfo::TranslationsPath); +#endif + QString translationsFileName = QStringLiteral("qt_") + locale.name(); + + if (qtTranslator_.load(translationsFileName, translationsPath)) + installTranslator(&qtTranslator_); + + // E.g. "_en" + translationsFileName + = QCoreApplication::applicationName().toLower() + '_' + locale.name(); + + // Try first in the same binary directory, in case we are building, + // otherwise read from system data + translationsPath = QCoreApplication::applicationDirPath(); + + bool isLoaded = translator_.load(translationsFileName, translationsPath); + if (!isLoaded) { + // "/usr/share//translations + isLoaded = translator_.load(translationsFileName, + QStringLiteral(PROJECT_DATA_DIR) + + QStringLiteral("/translations")); + } + if (isLoaded) + installTranslator(&translator_); +} + +void QRuler::Application::initUi() +{ settings_.load(); - mainWindow_ = new MainWindow; - dlgPrefs_ = new DialogPrefs(mainWindow_); + + mainWindow_ = new QRuler::MainWindow; + dlgPrefs_ = new QRuler::DialogPrefs(mainWindow_); + + mainWindow_->move(settings_.position()); + mainWindow_->resize(settings_.size()); + mainWindow_->show(); connect(dlgPrefs_, &QDialog::accepted, mainWindow_, &MainWindow::loadSettings); @@ -47,8 +107,6 @@ QRuler::Application::Application(int &argc, char **argv) mainWindow_->saveSettings(); settings_.save(); }); - // no need to call mainWindow_->show() because MainWindow::loadSettings() - // does it when setting its flags and it is called in its constructor } void QRuler::Application::preferences() @@ -59,24 +117,6 @@ void QRuler::Application::preferences() int main(int argc, char *argv[]) { - using namespace QRuler; - - Application app(argc, argv); - QTranslator qtTranslator, translator; - QString filename = QStringLiteral("qt_") + QLocale::system().name(); -#if QT_VERSION < 0x060000 - QString dir = QLibraryInfo::location(QLibraryInfo::TranslationsPath); -#else - QString dir = QLibraryInfo::path(QLibraryInfo::TranslationsPath); -#endif - if (qtTranslator.load(filename, dir)) - app.installTranslator(&qtTranslator); - - filename = QStringLiteral(PROJECT_ID) + '_' + QLocale::system().name(); - dir = QStringLiteral(PROJECT_DATA_DIR) + QStringLiteral("/translations"); - - if (translator.load(filename, dir)) - app.installTranslator(&translator); - + QRuler::Application app(argc, argv); return app.exec(); } diff --git a/src/application.hpp b/src/application.hpp index 86a2f54..08fb946 100644 --- a/src/application.hpp +++ b/src/application.hpp @@ -20,6 +20,8 @@ #include "settings.hpp" #include +#include +#include namespace QRuler { class MainWindow; @@ -29,13 +31,21 @@ class Application : public QApplication Q_OBJECT public: - Application(int &argc, char **argv); + Application(int argc, char *argv[]); Settings &settings() { return settings_; } + QIcon icon() const { return appIcon_; } void preferences(); private: + void initLocale(); + void initUi(); + MainWindow *mainWindow_; DialogPrefs *dlgPrefs_; Settings settings_; + + QIcon appIcon_; + QTranslator qtTranslator_; + QTranslator translator_; }; } // namespace QRuler diff --git a/src/dialogabout.cpp b/src/dialogabout.cpp index b4e80de..1b2d025 100644 --- a/src/dialogabout.cpp +++ b/src/dialogabout.cpp @@ -7,7 +7,7 @@ QRuler::DialogAbout::DialogAbout(QWidget *parent) : QDialog(parent) - , ui(new Ui::DialogAbout) + , ui(new QRuler::Ui::DialogAbout) { ui->setupUi(this); ui->tabInfo->setLayout(ui->layTabInfo); @@ -15,7 +15,8 @@ QRuler::DialogAbout::DialogAbout(QWidget *parent) ui->tabLicense->setLayout(ui->layTabLicense); // TODO: Probably needed only on X11 - Settings &settings = static_cast(qApp)->settings(); + Application *theApp = static_cast(qApp); + Settings &settings = theApp->settings(); if (settings.alwaysOnTop()) { Qt::WindowFlags flags = windowFlags(); flags |= Qt::WindowStaysOnTopHint; @@ -24,6 +25,9 @@ QRuler::DialogAbout::DialogAbout(QWidget *parent) connect(ui->buttonBox, &QDialogButtonBox::clicked, this, &QRuler::DialogAbout::close); + + setWindowIcon(theApp->icon()); + setWindowTitle(tr("About")); } QRuler::DialogAbout::~DialogAbout() { delete ui; } diff --git a/src/dialogabout.hpp b/src/dialogabout.hpp index 9d97acc..301eeb3 100644 --- a/src/dialogabout.hpp +++ b/src/dialogabout.hpp @@ -2,10 +2,10 @@ #include +namespace QRuler { namespace Ui { class DialogAbout; } -namespace QRuler { class DialogAbout : public QDialog { Q_OBJECT diff --git a/src/dialogabout.ui b/src/dialogabout.ui index 2999344..d909bec 100644 --- a/src/dialogabout.ui +++ b/src/dialogabout.ui @@ -1,7 +1,7 @@ - DialogAbout - + QRuler::DialogAbout + 0 diff --git a/src/dialogprefs.cpp b/src/dialogprefs.cpp index b5d56a9..9099635 100644 --- a/src/dialogprefs.cpp +++ b/src/dialogprefs.cpp @@ -25,7 +25,7 @@ QRuler::DialogPrefs::DialogPrefs(QWidget *parent) : QDialog(parent) - , ui(new Ui::DialogPrefs) + , ui(new QRuler::Ui::DialogPrefs) { ui->setupUi(this); @@ -47,9 +47,10 @@ QRuler::DialogPrefs::DialogPrefs(QWidget *parent) connect(ui->pbnFg, &QAbstractButton::clicked, this, [this]() { setColorForLabel(ui->lblColorFg); }); - setWindowIcon(QIcon(":/appicon")); + Application *theApp = static_cast(qApp); + + setWindowIcon(theApp->icon()); setWindowTitle(tr("Preferences")); - setFixedSize(384, 256); } QRuler::DialogPrefs::~DialogPrefs() { delete ui; } diff --git a/src/dialogprefs.hpp b/src/dialogprefs.hpp index 240716c..ab9694a 100644 --- a/src/dialogprefs.hpp +++ b/src/dialogprefs.hpp @@ -21,10 +21,10 @@ class QLabel; +namespace QRuler { namespace Ui { class DialogPrefs; } -namespace QRuler { class DialogPrefs : public QDialog { Q_OBJECT diff --git a/src/dialogprefs.ui b/src/dialogprefs.ui index fd0dd29..e08c8f4 100644 --- a/src/dialogprefs.ui +++ b/src/dialogprefs.ui @@ -1,7 +1,7 @@ - DialogPrefs - + QRuler::DialogPrefs + 0 @@ -10,153 +10,169 @@ 256 - - - - 10 - 200 - 360 - 48 - - - - - 0 - 0 - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - - - - 10 - 10 - 360 - 192 - - - - 0 - - - - General - - - - - 10 - 10 - 341 - 141 - + + + + + 0 - - - - - Opacity: - - - - - - - - 0 - 0 - - - - 1.000000000000000 - - - 0.050000000000000 - - - - - - - Always on top - - - - + + + General + + + + + 10 + 10 + 341 + 141 + + + + + + + + 0 + 0 + + + + 1.000000000000000 + + + 0.050000000000000 + + + + + + + Opacity: + + + + + + + Always on top + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + Colors + + + + + 10 + 10 + 341 + 141 + + + + + + + Text + + + + + + + + + + + + + + Background + + + + + + + + + + + + + + Border + + + + + + + + 0 + 0 + + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + - - - - Colors - - - - - 10 - 10 - 341 - 141 - + + + + + + 0 + 0 + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - - - Background - - - - - - - - - - - - - - - 0 - 0 - - - - - - - - - - - - - - - - - - Border - - - - - - - Text - - - - - - + + diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index a5eeb78..2228971 100755 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -23,6 +23,8 @@ #include #include #include +#include +#include #include #include #include @@ -48,26 +50,15 @@ QRuler::MainWindow::MainWindow(QWidget *parent) Application *theApp = static_cast(qApp); - QAction *actPrefs = mnuContext_->addAction(tr("&Preferences"), theApp, - &Application::preferences); - QAction *actAbout - = mnuContext_->addAction(tr("&About"), this, &MainWindow::about); - QAction *actQuit - = mnuContext_->addAction(tr("&Quit"), qApp, &QCoreApplication::quit); - - actPrefs->setShortcuts(QKeySequence::Preferences); - actQuit->setShortcuts(QKeySequence::Quit); - - actAbout->setIcon(iconAbout); - actPrefs->setIcon(iconPrefs); - actQuit->setIcon(iconQuit); - - mnuContext_->addAction(actAbout); - mnuContext_->addAction(actPrefs); + mnuContext_->addAction(iconAbout, tr("&About"), this, &MainWindow::about); + mnuContext_->addAction(iconPrefs, tr("&Preferences"), theApp, + &Application::preferences, + QKeySequence::Preferences); mnuContext_->addSeparator(); - mnuContext_->addAction(actQuit); + mnuContext_->addAction(iconQuit, tr("&Quit"), &QCoreApplication::quit, + QKeySequence::Quit); - setWindowIcon(QIcon(":/appicon")); + setWindowIcon(theApp->icon()); setMouseTracking(true); loadSettings(); } @@ -80,12 +71,11 @@ void QRuler::MainWindow::loadSettings() setAttribute(Qt::WA_TranslucentBackground); setWindowOpacity(opacity); + if (settings.alwaysOnTop()) flags |= Qt::WindowStaysOnTopHint; setWindowFlags(flags); - resize(settings.size()); - move(settings.position()); show(); // update the flags } diff --git a/src/settings.hpp b/src/settings.hpp index 1320495..c3b7815 100644 --- a/src/settings.hpp +++ b/src/settings.hpp @@ -21,12 +21,11 @@ #include #include #include +#include namespace QRuler { - class Settings { - public: Settings();