Big refactoring of toolbars.

This commit is contained in:
Martin Rotter 2017-06-16 12:31:42 +02:00
parent f01de0b93a
commit 972dd86d4c
12 changed files with 146 additions and 59 deletions

View File

@ -35,6 +35,10 @@ BaseToolBar::~BaseToolBar() {
qDebug("Destroying BaseToolBar instance."); qDebug("Destroying BaseToolBar instance.");
} }
void BaseBar::loadSavedActions() {
loadSpecificActions(getSpecificActions(savedActions()));
}
QAction *BaseBar::findMatchingAction(const QString &action, const QList<QAction*> &actions) const { QAction *BaseBar::findMatchingAction(const QString &action, const QList<QAction*> &actions) const {
foreach (QAction *act, actions) { foreach (QAction *act, actions) {
if (act->objectName() == action) { if (act->objectName() == action) {

View File

@ -34,8 +34,15 @@ class BaseBar {
// state into the settings. // state into the settings.
virtual void saveChangeableActions(const QStringList &actions) = 0; virtual void saveChangeableActions(const QStringList &actions) = 0;
// Returns list of default actions.
virtual QStringList defaultActions() const = 0;
virtual QStringList savedActions() const = 0;
// Loads the toolbar state from settings. // Loads the toolbar state from settings.
virtual void loadChangeableActions() = 0; virtual void loadSavedActions();
virtual QList<QAction*> getSpecificActions(const QStringList &actions) = 0;
virtual void loadSpecificActions(const QList<QAction*> &actions) = 0;
protected: protected:
QAction *findMatchingAction(const QString &action, const QList<QAction *> &actions) const; QAction *findMatchingAction(const QString &action, const QList<QAction *> &actions) const;

View File

@ -74,8 +74,8 @@ FormMain::FormMain(QWidget *parent, Qt::WindowFlags f)
// Prepare tabs. // Prepare tabs.
//m_ui->m_tabWidget->initializeTabs(); //m_ui->m_tabWidget->initializeTabs();
tabWidget()->feedMessageViewer()->feedsToolBar()->loadChangeableActions(); tabWidget()->feedMessageViewer()->feedsToolBar()->loadSavedActions();
tabWidget()->feedMessageViewer()->messagesToolBar()->loadChangeableActions(); tabWidget()->feedMessageViewer()->messagesToolBar()->loadSavedActions();
// Establish connections. // Establish connections.
createConnections(); createConnections();
@ -87,7 +87,7 @@ FormMain::FormMain(QWidget *parent, Qt::WindowFlags f)
setupIcons(); setupIcons();
loadSize(); loadSize();
m_statusBar->loadChangeableActions(); m_statusBar->loadSavedActions();
} }
FormMain::~FormMain() { FormMain::~FormMain() {

View File

@ -229,8 +229,8 @@ void FeedMessageViewer::initialize() {
m_toolBarMessages->setMovable(false); m_toolBarMessages->setMovable(false);
m_toolBarMessages->setAllowedAreas(Qt::TopToolBarArea); m_toolBarMessages->setAllowedAreas(Qt::TopToolBarArea);
m_toolBarFeeds->loadChangeableActions(); m_toolBarFeeds->loadSavedActions();
m_toolBarMessages->loadChangeableActions(); m_toolBarMessages->loadSavedActions();
m_messagesBrowser->clear(); m_messagesBrowser->clear();

View File

@ -21,6 +21,8 @@
#include "miscellaneous/settings.h" #include "miscellaneous/settings.h"
#include "miscellaneous/iconfactory.h" #include "miscellaneous/iconfactory.h"
#include <QWidgetAction>
FeedsToolBar::FeedsToolBar(const QString &title, QWidget *parent) : BaseToolBar(title, parent) { FeedsToolBar::FeedsToolBar(const QString &title, QWidget *parent) : BaseToolBar(title, parent) {
// Update right margin of filter textbox. // Update right margin of filter textbox.
@ -42,20 +44,12 @@ QList<QAction*> FeedsToolBar::changeableActions() const {
void FeedsToolBar::saveChangeableActions(const QStringList &actions) { void FeedsToolBar::saveChangeableActions(const QStringList &actions) {
qApp->settings()->setValue(GROUP(GUI), GUI::FeedsToolbarActions, actions.join(QSL(","))); qApp->settings()->setValue(GROUP(GUI), GUI::FeedsToolbarActions, actions.join(QSL(",")));
loadChangeableActions(actions); loadSpecificActions(getSpecificActions(actions));
} }
void FeedsToolBar::loadChangeableActions() { QList<QAction*> FeedsToolBar::getSpecificActions(const QStringList &actions) {
QStringList action_names = qApp->settings()->value(GROUP(GUI), SETTING(GUI::FeedsToolbarActions)).toString().split(',',
QString::SkipEmptyParts);
loadChangeableActions(action_names);
}
void FeedsToolBar::loadChangeableActions(const QStringList &actions) {
QList<QAction*> available_actions = availableActions(); QList<QAction*> available_actions = availableActions();
QList<QAction*> spec_actions;
clear();
// Iterate action names and add respectable actions into the toolbar. // Iterate action names and add respectable actions into the toolbar.
foreach (const QString &action_name, actions) { foreach (const QString &action_name, actions) {
@ -63,21 +57,49 @@ void FeedsToolBar::loadChangeableActions(const QStringList &actions) {
if (matching_action != nullptr) { if (matching_action != nullptr) {
// Add existing standard action. // Add existing standard action.
addAction(matching_action); spec_actions.append(matching_action);
} }
else if (action_name == SEPARATOR_ACTION_NAME) { else if (action_name == SEPARATOR_ACTION_NAME) {
// Add new separator. // Add new separator.
addSeparator(); QAction *act = new QAction(this);
act->setSeparator(true);
spec_actions.append(act);
} }
else if (action_name == SPACER_ACTION_NAME) { else if (action_name == SPACER_ACTION_NAME) {
// Add new spacer. // Add new spacer.
QWidget *spacer = new QWidget(this); QWidget *spacer = new QWidget(this);
spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QAction *action = addWidget(spacer); QWidgetAction *action = new QWidgetAction(this);
action->setDefaultWidget(spacer);
action->setIcon(qApp->icons()->fromTheme(QSL("system-search"))); action->setIcon(qApp->icons()->fromTheme(QSL("system-search")));
action->setProperty("type", SPACER_ACTION_NAME); action->setProperty("type", SPACER_ACTION_NAME);
action->setProperty("name", tr("Toolbar spacer")); action->setProperty("name", tr("Toolbar spacer"));
spec_actions.append(action);
} }
} }
return spec_actions;
}
void FeedsToolBar::loadSpecificActions(const QList<QAction*> &actions) {
clear();
foreach (QAction *act, actions) {
addAction(act);
}
}
QStringList FeedsToolBar::defaultActions() const {
return QString(GUI::FeedsToolbarActionsDef).split(',',
QString::SkipEmptyParts);
}
QStringList FeedsToolBar::savedActions() const {
return qApp->settings()->value(GROUP(GUI), SETTING(GUI::FeedsToolbarActions)).toString().split(',',
QString::SkipEmptyParts);
} }

View File

@ -32,12 +32,12 @@ class FeedsToolBar : public BaseToolBar {
QList<QAction*> availableActions() const; QList<QAction*> availableActions() const;
QList<QAction*> changeableActions() const; QList<QAction*> changeableActions() const;
void saveChangeableActions(const QStringList &actions); void saveChangeableActions(const QStringList &actions);
void loadChangeableActions();
// Loads actions as specified by external actions list. QList<QAction*> getSpecificActions(const QStringList &actions);
// NOTE: This is used primarily for reloading actions void loadSpecificActions(const QList<QAction*> &actions);
// when they are changed from settings.
void loadChangeableActions(const QStringList &actions); QStringList defaultActions() const;
QStringList savedActions() const;
}; };
#endif // FEEDSTOOLBAR_H #endif // FEEDSTOOLBAR_H

View File

@ -52,7 +52,7 @@ QList<QAction*> MessagesToolBar::changeableActions() const {
void MessagesToolBar::saveChangeableActions(const QStringList& actions) { void MessagesToolBar::saveChangeableActions(const QStringList& actions) {
qApp->settings()->setValue(GROUP(GUI), GUI::MessagesToolbarDefaultButtons, actions.join(QSL(","))); qApp->settings()->setValue(GROUP(GUI), GUI::MessagesToolbarDefaultButtons, actions.join(QSL(",")));
loadChangeableActions(actions); loadSpecificActions(getSpecificActions(actions));
// If user hidden search messages box, then remove the filter. // If user hidden search messages box, then remove the filter.
if (!changeableActions().contains(m_actionSearchMessages)) { if (!changeableActions().contains(m_actionSearchMessages)) {
@ -60,10 +60,9 @@ void MessagesToolBar::saveChangeableActions(const QStringList& actions) {
} }
} }
void MessagesToolBar::loadChangeableActions(const QStringList& actions) { QList<QAction*> MessagesToolBar::getSpecificActions(const QStringList &actions) {
QList<QAction*> available_actions = availableActions(); QList<QAction*> available_actions = availableActions();
QList<QAction*> spec_actions;
clear();
// Iterate action names and add respectable actions into the toolbar. // Iterate action names and add respectable actions into the toolbar.
foreach (const QString &action_name, actions) { foreach (const QString &action_name, actions) {
@ -71,31 +70,48 @@ void MessagesToolBar::loadChangeableActions(const QStringList& actions) {
if (matching_action != nullptr) { if (matching_action != nullptr) {
// Add existing standard action. // Add existing standard action.
addAction(matching_action); spec_actions.append(matching_action);
} }
else if (action_name == SEPARATOR_ACTION_NAME) { else if (action_name == SEPARATOR_ACTION_NAME) {
// Add new separator. // Add new separator.
addSeparator(); QAction *act = new QAction(this);
act->setSeparator(true);
spec_actions.append(act);
} }
else if (action_name == SEACRH_MESSAGES_ACTION_NAME) { else if (action_name == SEACRH_MESSAGES_ACTION_NAME) {
// Add search box. // Add search box.
addAction(m_actionSearchMessages); spec_actions.append(m_actionSearchMessages);
} }
else if (action_name == HIGHLIGHTER_ACTION_NAME) { else if (action_name == HIGHLIGHTER_ACTION_NAME) {
// Add filter button. // Add filter button.
addAction(m_actionMessageHighlighter); spec_actions.append(m_actionMessageHighlighter);
} }
else if (action_name == SPACER_ACTION_NAME) { else if (action_name == SPACER_ACTION_NAME) {
// Add new spacer. // Add new spacer.
QWidget *spacer = new QWidget(this); QWidget *spacer = new QWidget(this);
spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QAction *action = addWidget(spacer); QWidgetAction *action = new QWidgetAction(this);
action->setDefaultWidget(spacer);
action->setIcon(qApp->icons()->fromTheme(QSL("go-jump"))); action->setIcon(qApp->icons()->fromTheme(QSL("go-jump")));
action->setProperty("type", SPACER_ACTION_NAME); action->setProperty("type", SPACER_ACTION_NAME);
action->setProperty("name", tr("Toolbar spacer")); action->setProperty("name", tr("Toolbar spacer"));
spec_actions.append(action);
} }
} }
return spec_actions;
}
void MessagesToolBar::loadSpecificActions(const QList<QAction*> &actions) {
clear();
foreach (QAction *act, actions) {
addAction(act);
}
} }
void MessagesToolBar::handleMessageHighlighterChange(QAction *action) { void MessagesToolBar::handleMessageHighlighterChange(QAction *action) {
@ -145,10 +161,13 @@ void MessagesToolBar::initializeHighlighter() {
this, SLOT(handleMessageHighlighterChange(QAction*))); this, SLOT(handleMessageHighlighterChange(QAction*)));
} }
void MessagesToolBar::loadChangeableActions() { QStringList MessagesToolBar::defaultActions() const {
QStringList action_names = qApp->settings()->value(GROUP(GUI), return QString(GUI::MessagesToolbarDefaultButtonsDef).split(',',
QString::SkipEmptyParts);
}
QStringList MessagesToolBar::savedActions() const {
return qApp->settings()->value(GROUP(GUI),
SETTING(GUI::MessagesToolbarDefaultButtons)).toString().split(',', SETTING(GUI::MessagesToolbarDefaultButtons)).toString().split(',',
QString::SkipEmptyParts); QString::SkipEmptyParts);
loadChangeableActions(action_names);
} }

View File

@ -45,12 +45,16 @@ class MessagesToolBar : public BaseToolBar {
QList<QAction*> availableActions() const; QList<QAction*> availableActions() const;
QList<QAction*> changeableActions() const; QList<QAction*> changeableActions() const;
void saveChangeableActions(const QStringList &actions); void saveChangeableActions(const QStringList &actions);
void loadChangeableActions();
// Loads actions as specified by external actions list. // Loads actions as specified by external actions list.
// NOTE: This is used primarily for reloading actions // NOTE: This is used primarily for reloading actions
// when they are changed from settings. // when they are changed from settings.
void loadChangeableActions(const QStringList &actions); void loadSpecificActions(const QList<QAction*> &actions);
QList<QAction*> getSpecificActions(const QStringList &actions);
QStringList defaultActions() const;
QStringList savedActions() const;
signals: signals:
void messageSearchPatternChanged(const QString &pattern); void messageSearchPatternChanged(const QString &pattern);

View File

@ -94,28 +94,26 @@ void StatusBar::saveChangeableActions(const QStringList &actions) {
QMutexLocker locker(*m_mutex); QMutexLocker locker(*m_mutex);
qApp->settings()->setValue(GROUP(GUI), GUI::StatusbarActions, actions.join(QSL(","))); qApp->settings()->setValue(GROUP(GUI), GUI::StatusbarActions, actions.join(QSL(",")));
loadChangeableActions(actions); loadSpecificActions(getSpecificActions(actions));
} }
void StatusBar::loadChangeableActions() { QStringList StatusBar::defaultActions() const {
QMutexLocker locker(*m_mutex); return QString(GUI::StatusbarActionsDef).split(',', QString::SkipEmptyParts);
QStringList action_names = qApp->settings()->value(GROUP(GUI), SETTING(GUI::StatusbarActions)).toString().split(',',
QString::SkipEmptyParts);
loadChangeableActions(action_names);
} }
void StatusBar::loadChangeableActions(const QStringList &action_names) { QStringList StatusBar::savedActions() const {
clear(); return qApp->settings()->value(GROUP(GUI), SETTING(GUI::StatusbarActions)).toString().split(',', QString::SkipEmptyParts);
}
bool progress_visible = actions().contains(m_barProgressFeedsAction) && QList<QAction*> StatusBar::getSpecificActions(const QStringList &actions) {
bool progress_visible = this->actions().contains(m_barProgressFeedsAction) &&
m_lblProgressFeeds->isVisible() && m_lblProgressFeeds->isVisible() &&
m_barProgressFeeds->isVisible(); m_barProgressFeeds->isVisible();
QList<QAction*> available_actions = availableActions(); QList<QAction*> available_actions = availableActions();
QList<QAction*> spec_actions;
// Iterate action names and add respectable actions into the toolbar. // Iterate action names and add respectable actions into the toolbar.
foreach (const QString &action_name, action_names) { foreach (const QString &action_name, actions) {
QAction *matching_action = findMatchingAction(action_name, available_actions); QAction *matching_action = findMatchingAction(action_name, available_actions);
QAction *action_to_add; QAction *action_to_add;
QWidget *widget_to_add; QWidget *widget_to_add;
@ -186,8 +184,32 @@ void StatusBar::loadChangeableActions(const QStringList &action_names) {
if (action_to_add != nullptr && widget_to_add != nullptr) { if (action_to_add != nullptr && widget_to_add != nullptr) {
action_to_add->setProperty("widget", QVariant::fromValue((void*) widget_to_add)); action_to_add->setProperty("widget", QVariant::fromValue((void*) widget_to_add));
addPermanentWidget(widget_to_add); spec_actions.append(action_to_add);
addAction(action_to_add); }
}
return spec_actions;
}
void StatusBar::loadSpecificActions(const QList<QAction*> &actions) {
foreach (QAction *act, this->actions()) {
QWidget *widget = act->property("widget").isValid() ? static_cast<QWidget*>(act->property("widget").value<void*>()) : nullptr;
if (widget != nullptr) {
removeWidget(widget);
}
}
clear();
foreach (QAction *act, actions) {
QWidget *widget = act->property("widget").isValid() ? static_cast<QWidget*>(act->property("widget").value<void*>()) : nullptr;
addAction(act);
// And also add widget.
if (widget != nullptr) {
addPermanentWidget(widget);
} }
} }
} }

View File

@ -39,7 +39,10 @@ class StatusBar : public QStatusBar, public BaseBar {
QList<QAction*> availableActions() const; QList<QAction*> availableActions() const;
QList<QAction*> changeableActions() const; QList<QAction*> changeableActions() const;
void saveChangeableActions(const QStringList &actions); void saveChangeableActions(const QStringList &actions);
void loadChangeableActions(); QStringList defaultActions() const;
QStringList savedActions() const;
QList<QAction*> getSpecificActions(const QStringList &actions);
void loadSpecificActions(const QList<QAction*> &actions);
public slots: public slots:
// Progress bar operations // Progress bar operations
@ -54,7 +57,6 @@ class StatusBar : public QStatusBar, public BaseBar {
private: private:
void clear(); void clear();
void loadChangeableActions(const QStringList &action_names);
Mutex *m_mutex; Mutex *m_mutex;

View File

@ -120,6 +120,12 @@ void ToolBarEditor::saveToolBar() {
m_toolBar->saveChangeableActions(action_names); m_toolBar->saveChangeableActions(action_names);
} }
void ToolBarEditor::resetToolBar() {
if (m_toolBar != nullptr) {
}
}
bool ToolBarEditor::eventFilter(QObject *object, QEvent *event) { bool ToolBarEditor::eventFilter(QObject *object, QEvent *event) {
if (object == m_ui->m_listActivatedActions) { if (object == m_ui->m_listActivatedActions) {
if (event->type() == QEvent::KeyPress) { if (event->type() == QEvent::KeyPress) {

View File

@ -40,6 +40,7 @@ class ToolBarEditor : public QWidget {
// Toolbar operations. // Toolbar operations.
void loadFromToolBar(BaseBar *tool_bar); void loadFromToolBar(BaseBar *tool_bar);
void saveToolBar(); void saveToolBar();
void resetToolBar();
inline QListWidget *activeItemsWidget() const { inline QListWidget *activeItemsWidget() const {
return m_ui->m_listActivatedActions; return m_ui->m_listActivatedActions;