From 0ebfea6c68f6146d00e812a75b5575750e4e6e1d Mon Sep 17 00:00:00 2001 From: Gabriele Date: Sat, 18 Oct 2014 15:18:58 +0100 Subject: [PATCH] Add a context menu to the toolbar to change the position of the text Closes #1145. --- src/mainwindow.cpp | 94 +++++++++++++++++++++++++++++++++ src/mainwindow.h | 8 +++ src/preferences/preferences.cpp | 8 +++ src/preferences/preferences.h | 2 + 4 files changed, 112 insertions(+) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 9d0b93484..fc44ab223 100755 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -124,6 +124,8 @@ MainWindow::MainWindow(QWidget *parent, const QStringList& torrentCmdLine): QMai #endif setWindowIcon(QIcon(QString::fromUtf8(":/Icons/skin/qbittorrent32.png"))); + addToolbarContextMenu(); + actionOpen->setIcon(IconProvider::instance()->getIcon("list-add")); actionDownload_from_URL->setIcon(IconProvider::instance()->getIcon("insert-link")); actionSet_upload_limit->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/seeding.png"))); @@ -356,6 +358,97 @@ MainWindow::MainWindow(QWidget *parent, const QStringList& torrentCmdLine): QMai } } +void MainWindow::addToolbarContextMenu() +{ + const Preferences* const pref = Preferences::instance(); + toolbarMenu = new QMenu(this); + + toolBar->setContextMenuPolicy(Qt::CustomContextMenu); + connect(toolBar, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(toolbarMenuRequested(QPoint))); + + QAction *iconsOnly = new QAction(tr("Icons Only"), toolbarMenu); + connect(iconsOnly, SIGNAL(triggered()), this, SLOT(toolbarIconsOnly())); + QAction *textOnly = new QAction(tr("Text Only"), toolbarMenu); + connect(textOnly, SIGNAL(triggered()), this, SLOT(toolbarTextOnly())); + QAction *textBesideIcons = new QAction(tr("Text Alongside Icons"), toolbarMenu); + connect(textBesideIcons, SIGNAL(triggered()), this, SLOT(toolbarTextBeside())); + QAction *textUnderIcons = new QAction(tr("Text Under Icons"), toolbarMenu); + connect(textUnderIcons, SIGNAL(triggered()), this, SLOT(toolbarTextUnder())); + QAction *followSystemStyle = new QAction(tr("Follow System Style"), toolbarMenu); + connect(followSystemStyle, SIGNAL(triggered()), this, SLOT(toolbarFollowSystem())); + toolbarMenu->addAction(iconsOnly); + toolbarMenu->addAction(textOnly); + toolbarMenu->addAction(textBesideIcons); + toolbarMenu->addAction(textUnderIcons); + toolbarMenu->addAction(followSystemStyle); + QActionGroup *textPositionGroup = new QActionGroup(toolbarMenu); + textPositionGroup->addAction(iconsOnly); + iconsOnly->setCheckable(true); + textPositionGroup->addAction(textOnly); + textOnly->setCheckable(true); + textPositionGroup->addAction(textBesideIcons); + textBesideIcons->setCheckable(true); + textPositionGroup->addAction(textUnderIcons); + textUnderIcons->setCheckable(true); + textPositionGroup->addAction(followSystemStyle); + followSystemStyle->setCheckable(true); + + const Qt::ToolButtonStyle buttonStyle = static_cast(pref->getToolbarTextPosition()); + if (buttonStyle >= Qt::ToolButtonIconOnly && buttonStyle <= Qt::ToolButtonFollowStyle) + toolBar->setToolButtonStyle(buttonStyle); + switch (buttonStyle) { + case Qt::ToolButtonIconOnly: + iconsOnly->setChecked(true); + break; + case Qt::ToolButtonTextOnly: + textOnly->setChecked(true); + break; + case Qt::ToolButtonTextBesideIcon: + textBesideIcons->setChecked(true); + break; + case Qt::ToolButtonTextUnderIcon: + textUnderIcons->setChecked(true); + break; + default: + followSystemStyle->setChecked(true); + } +} + +void MainWindow::toolbarMenuRequested(QPoint point) +{ + toolbarMenu->exec(toolBar->mapToGlobal(point)); +} + +void MainWindow::toolbarIconsOnly() +{ + toolBar->setToolButtonStyle(Qt::ToolButtonIconOnly); + Preferences::instance()->setToolbarTextPosition(Qt::ToolButtonIconOnly); +} + +void MainWindow::toolbarTextOnly() +{ + toolBar->setToolButtonStyle(Qt::ToolButtonTextOnly); + Preferences::instance()->setToolbarTextPosition(Qt::ToolButtonTextOnly); +} + +void MainWindow::toolbarTextBeside() +{ + toolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); + Preferences::instance()->setToolbarTextPosition(Qt::ToolButtonTextBesideIcon); +} + +void MainWindow::toolbarTextUnder() +{ + toolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); + Preferences::instance()->setToolbarTextPosition(Qt::ToolButtonTextUnderIcon); +} + +void MainWindow::toolbarFollowSystem() +{ + toolBar->setToolButtonStyle(Qt::ToolButtonFollowStyle); + Preferences::instance()->setToolbarTextPosition(Qt::ToolButtonFollowStyle); +} + void MainWindow::shutdownCleanUp() { qDebug("GUI destruction"); @@ -410,6 +503,7 @@ void MainWindow::shutdownCleanUp() delete switchSearchShortcut2; delete switchTransferShortcut; delete switchRSSShortcut; + delete toolbarMenu; IconProvider::drop(); Preferences::drop(); qDebug("Finished GUI destruction"); diff --git a/src/mainwindow.h b/src/mainwindow.h index e073e6e77..b18559ba1 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -161,6 +161,7 @@ private slots: void pythonDownloadSuccess(QString url, QString file_path); void pythonDownloadFailure(QString url, QString error); #endif + void addToolbarContextMenu(); private: QFileSystemWatcher *executable_watcher; @@ -214,6 +215,7 @@ private: #ifdef Q_OS_WIN bool has_python; #endif + QMenu* toolbarMenu; private slots: void on_actionSearch_engine_triggered(); @@ -232,6 +234,12 @@ private slots: #if defined(Q_OS_WIN) || defined(Q_OS_MAC) void checkProgramUpdate(); #endif + void toolbarMenuRequested(QPoint); + void toolbarIconsOnly(); + void toolbarTextOnly(); + void toolbarTextBeside(); + void toolbarTextUnder(); + void toolbarFollowSystem(); }; #endif diff --git a/src/preferences/preferences.cpp b/src/preferences/preferences.cpp index 5b57cf61c..fb9518a99 100644 --- a/src/preferences/preferences.cpp +++ b/src/preferences/preferences.cpp @@ -1952,6 +1952,14 @@ void Preferences::setRssFeedsAliases(const QStringList &rssAliases) { setValue("Rss/streamAlias", rssAliases); } +int Preferences::getToolbarTextPosition() const { + return value("Toolbar/textPosition", -1).toInt(); +} + +void Preferences::setToolbarTextPosition(const int position) { + setValue("Toolbar/textPosition", position); +} + QList Preferences::getHostNameCookies(const QString &host_name) const { QMap hosts_table = value("Rss/hosts_cookies").toMap(); if (!hosts_table.contains(host_name)) return QList(); diff --git a/src/preferences/preferences.h b/src/preferences/preferences.h index 079d63b81..487aae02e 100755 --- a/src/preferences/preferences.h +++ b/src/preferences/preferences.h @@ -460,6 +460,8 @@ public: void setTransSelFilter(const int &index); QByteArray getTransHeaderState() const; void setTransHeaderState(const QByteArray &state); + int getToolbarTextPosition() const; + void setToolbarTextPosition(const int position); // Temp code. // See TorrentStatistics::loadStats() for details.