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.");
}
void BaseBar::loadSavedActions() {
loadSpecificActions(getSpecificActions(savedActions()));
}
QAction *BaseBar::findMatchingAction(const QString &action, const QList<QAction*> &actions) const {
foreach (QAction *act, actions) {
if (act->objectName() == action) {

View File

@ -34,8 +34,15 @@ class BaseBar {
// state into the settings.
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.
virtual void loadChangeableActions() = 0;
virtual void loadSavedActions();
virtual QList<QAction*> getSpecificActions(const QStringList &actions) = 0;
virtual void loadSpecificActions(const QList<QAction*> &actions) = 0;
protected:
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.
//m_ui->m_tabWidget->initializeTabs();
tabWidget()->feedMessageViewer()->feedsToolBar()->loadChangeableActions();
tabWidget()->feedMessageViewer()->messagesToolBar()->loadChangeableActions();
tabWidget()->feedMessageViewer()->feedsToolBar()->loadSavedActions();
tabWidget()->feedMessageViewer()->messagesToolBar()->loadSavedActions();
// Establish connections.
createConnections();
@ -87,7 +87,7 @@ FormMain::FormMain(QWidget *parent, Qt::WindowFlags f)
setupIcons();
loadSize();
m_statusBar->loadChangeableActions();
m_statusBar->loadSavedActions();
}
FormMain::~FormMain() {

View File

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

View File

@ -21,6 +21,8 @@
#include "miscellaneous/settings.h"
#include "miscellaneous/iconfactory.h"
#include <QWidgetAction>
FeedsToolBar::FeedsToolBar(const QString &title, QWidget *parent) : BaseToolBar(title, parent) {
// Update right margin of filter textbox.
@ -42,20 +44,12 @@ QList<QAction*> FeedsToolBar::changeableActions() const {
void FeedsToolBar::saveChangeableActions(const QStringList &actions) {
qApp->settings()->setValue(GROUP(GUI), GUI::FeedsToolbarActions, actions.join(QSL(",")));
loadChangeableActions(actions);
loadSpecificActions(getSpecificActions(actions));
}
void FeedsToolBar::loadChangeableActions() {
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*> FeedsToolBar::getSpecificActions(const QStringList &actions) {
QList<QAction*> available_actions = availableActions();
clear();
QList<QAction*> spec_actions;
// Iterate action names and add respectable actions into the toolbar.
foreach (const QString &action_name, actions) {
@ -63,21 +57,49 @@ void FeedsToolBar::loadChangeableActions(const QStringList &actions) {
if (matching_action != nullptr) {
// Add existing standard action.
addAction(matching_action);
spec_actions.append(matching_action);
}
else if (action_name == SEPARATOR_ACTION_NAME) {
// Add new separator.
addSeparator();
QAction *act = new QAction(this);
act->setSeparator(true);
spec_actions.append(act);
}
else if (action_name == SPACER_ACTION_NAME) {
// Add new spacer.
QWidget *spacer = new QWidget(this);
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->setProperty("type", SPACER_ACTION_NAME);
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*> changeableActions() const;
void saveChangeableActions(const QStringList &actions);
void loadChangeableActions();
// Loads actions as specified by external actions list.
// NOTE: This is used primarily for reloading actions
// when they are changed from settings.
void loadChangeableActions(const QStringList &actions);
QList<QAction*> getSpecificActions(const QStringList &actions);
void loadSpecificActions(const QList<QAction*> &actions);
QStringList defaultActions() const;
QStringList savedActions() const;
};
#endif // FEEDSTOOLBAR_H

View File

@ -52,7 +52,7 @@ QList<QAction*> MessagesToolBar::changeableActions() const {
void MessagesToolBar::saveChangeableActions(const QStringList& actions) {
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 (!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();
clear();
QList<QAction*> spec_actions;
// Iterate action names and add respectable actions into the toolbar.
foreach (const QString &action_name, actions) {
@ -71,31 +70,48 @@ void MessagesToolBar::loadChangeableActions(const QStringList& actions) {
if (matching_action != nullptr) {
// Add existing standard action.
addAction(matching_action);
spec_actions.append(matching_action);
}
else if (action_name == SEPARATOR_ACTION_NAME) {
// Add new separator.
addSeparator();
QAction *act = new QAction(this);
act->setSeparator(true);
spec_actions.append(act);
}
else if (action_name == SEACRH_MESSAGES_ACTION_NAME) {
// Add search box.
addAction(m_actionSearchMessages);
spec_actions.append(m_actionSearchMessages);
}
else if (action_name == HIGHLIGHTER_ACTION_NAME) {
// Add filter button.
addAction(m_actionMessageHighlighter);
spec_actions.append(m_actionMessageHighlighter);
}
else if (action_name == SPACER_ACTION_NAME) {
// Add new spacer.
QWidget *spacer = new QWidget(this);
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->setProperty("type", SPACER_ACTION_NAME);
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) {
@ -145,10 +161,13 @@ void MessagesToolBar::initializeHighlighter() {
this, SLOT(handleMessageHighlighterChange(QAction*)));
}
void MessagesToolBar::loadChangeableActions() {
QStringList action_names = qApp->settings()->value(GROUP(GUI),
SETTING(GUI::MessagesToolbarDefaultButtons)).toString().split(',',
QString::SkipEmptyParts);
loadChangeableActions(action_names);
QStringList MessagesToolBar::defaultActions() const {
return QString(GUI::MessagesToolbarDefaultButtonsDef).split(',',
QString::SkipEmptyParts);
}
QStringList MessagesToolBar::savedActions() const {
return qApp->settings()->value(GROUP(GUI),
SETTING(GUI::MessagesToolbarDefaultButtons)).toString().split(',',
QString::SkipEmptyParts);
}

View File

@ -45,12 +45,16 @@ class MessagesToolBar : public BaseToolBar {
QList<QAction*> availableActions() const;
QList<QAction*> changeableActions() const;
void saveChangeableActions(const QStringList &actions);
void loadChangeableActions();
// Loads actions as specified by external actions list.
// NOTE: This is used primarily for reloading actions
// 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:
void messageSearchPatternChanged(const QString &pattern);

View File

@ -72,7 +72,7 @@ StatusBar::StatusBar(QWidget *parent) : QStatusBar(parent), m_mutex(new Mutex(QM
}
StatusBar::~StatusBar() {
clear();
clear();
qDebug("Destroying StatusBar instance.");
}
@ -94,28 +94,26 @@ void StatusBar::saveChangeableActions(const QStringList &actions) {
QMutexLocker locker(*m_mutex);
qApp->settings()->setValue(GROUP(GUI), GUI::StatusbarActions, actions.join(QSL(",")));
loadChangeableActions(actions);
loadSpecificActions(getSpecificActions(actions));
}
void StatusBar::loadChangeableActions() {
QMutexLocker locker(*m_mutex);
QStringList action_names = qApp->settings()->value(GROUP(GUI), SETTING(GUI::StatusbarActions)).toString().split(',',
QString::SkipEmptyParts);
loadChangeableActions(action_names);
QStringList StatusBar::defaultActions() const {
return QString(GUI::StatusbarActionsDef).split(',', QString::SkipEmptyParts);
}
void StatusBar::loadChangeableActions(const QStringList &action_names) {
clear();
QStringList StatusBar::savedActions() const {
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_barProgressFeeds->isVisible();
QList<QAction*> available_actions = availableActions();
QList<QAction*> spec_actions;
// 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 *action_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) {
action_to_add->setProperty("widget", QVariant::fromValue((void*) widget_to_add));
addPermanentWidget(widget_to_add);
addAction(action_to_add);
spec_actions.append(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*> changeableActions() const;
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:
// Progress bar operations
@ -54,7 +57,6 @@ class StatusBar : public QStatusBar, public BaseBar {
private:
void clear();
void loadChangeableActions(const QStringList &action_names);
Mutex *m_mutex;

View File

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

View File

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