diff --git a/CMakeLists.txt b/CMakeLists.txt index 15c8f3172..22bb54e81 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -371,6 +371,7 @@ if(BUILD_DESKTOP) part/xmlgui_helper.cpp part/extensions.cpp part/embeddedfilesdialog.cpp + part/actionbar.cpp part/annotationactionhandler.cpp part/annotwindow.cpp part/annotationmodel.cpp diff --git a/part/actionbar.cpp b/part/actionbar.cpp new file mode 100644 index 000000000..e1fcb83b4 --- /dev/null +++ b/part/actionbar.cpp @@ -0,0 +1,131 @@ +/*************************************************************************** + * Copyright (C) 2020 by Simone Gaiarin * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + ***************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "actionbar.h" + +class ActionBarWidget : public QWidget +{ + Q_OBJECT + +public: + ActionBarWidget(QToolBar *parent); + void recreateButtons(const QList &actions); + +private slots: + void onOrientationChanged(Qt::Orientation orientation); +}; + +ActionBarWidget::ActionBarWidget(QToolBar *parent) + : QWidget::QWidget(parent) +{ + QLayout *layout; + if (parent->orientation() == Qt::Vertical) { + layout = new QVBoxLayout(); + } else { + layout = new QHBoxLayout(); + } + setLayout(layout); + connect(parent, &QToolBar::orientationChanged, this, &ActionBarWidget::onOrientationChanged); +} + +void ActionBarWidget::recreateButtons(const QList &actions) +{ + QToolBar *parentToolbar = qobject_cast(parentWidget()); + if (!parentToolbar) { + return; + } + for (auto &toolButton : findChildren()) { + layout()->removeWidget(toolButton); + delete toolButton; + } + for (const auto &action : actions) { + QToolButton *toolButton = new QToolButton(this); + toolButton->setAutoRaise(true); + toolButton->setFocusPolicy(Qt::NoFocus); + toolButton->setIconSize(parentToolbar->iconSize()); + toolButton->setToolButtonStyle(parentToolbar->toolButtonStyle()); + toolButton->setDefaultAction(action); + layout()->addWidget(toolButton); + layout()->setAlignment(toolButton, Qt::AlignCenter); + connect(parentToolbar, &QToolBar::iconSizeChanged, toolButton, &QToolButton::setIconSize); + connect(parentToolbar, &QToolBar::toolButtonStyleChanged, toolButton, &QToolButton::setToolButtonStyle); + } +} + +void ActionBarWidget::onOrientationChanged(Qt::Orientation orientation) +{ + QLayout *newLayout; + if (orientation == Qt::Vertical) { + newLayout = new QVBoxLayout(); + } else { + newLayout = new QHBoxLayout(); + } + QLayout *oldLayout = layout(); + for (auto &toolButton : findChildren()) { + oldLayout->removeWidget(toolButton); + newLayout->addWidget(toolButton); + newLayout->setAlignment(toolButton, Qt::AlignCenter); + } + delete oldLayout; + setLayout(newLayout); +} + +ActionBar::ActionBar(QObject *parent) + : QWidgetAction(parent) +{ +} + +QWidget *ActionBar::createWidget(QWidget *parent) +{ + QToolBar *parentToolbar = qobject_cast(parent); + if (!parentToolbar) { + return new QWidget(); + } + ActionBarWidget *widget = new ActionBarWidget(parentToolbar); + widget->recreateButtons(m_actions); + return widget; +} + +void ActionBar::addAction(QAction *action) +{ + m_actions.append(action); +} + +void ActionBar::insertAction(int pos, QAction *action) +{ + m_actions.insert(pos, action); +} + +void ActionBar::removeAction(QAction *action) +{ + m_actions.removeAll(action); +} + +void ActionBar::recreateWidgets() +{ + const auto widgets = createdWidgets(); + for (auto *widget : widgets) { + auto *actionBarWidget = qobject_cast(widget); + if (actionBarWidget) { + actionBarWidget->recreateButtons(m_actions); + } + } +} + +#include "actionbar.moc" diff --git a/part/actionbar.h b/part/actionbar.h new file mode 100644 index 000000000..7ce9c5152 --- /dev/null +++ b/part/actionbar.h @@ -0,0 +1,38 @@ +/*************************************************************************** + * Copyright (C) 2020 by Simone Gaiarin * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + ***************************************************************************/ + +#ifndef ACTIONBAR_H +#define ACTIONBAR_H + +#include + +class QAction; +class QWidget; + +/** + * @short A widget action to display a set of actions in a toolbar + */ +class ActionBar : public QWidgetAction +{ + Q_OBJECT + +public: + ActionBar(QObject *parent = nullptr); + QWidget *createWidget(QWidget *parent) override; + + void addAction(QAction *action); + void insertAction(int pos, QAction *action); + void removeAction(QAction *action); + void recreateWidgets(); + +private: + QList m_actions; +}; + +#endif diff --git a/part/annotationactionhandler.cpp b/part/annotationactionhandler.cpp index 85ea68191..349d91187 100644 --- a/part/annotationactionhandler.cpp +++ b/part/annotationactionhandler.cpp @@ -25,6 +25,7 @@ #include // local includes +#include "actionbar.h" #include "annotationwidgets.h" #include "guiutils.h" #include "pageview.h" @@ -46,6 +47,7 @@ public: , agTools(nullptr) , agLastAction(nullptr) , aQuickTools(nullptr) + , aQuickToolsBar(nullptr) , aGeomShapes(nullptr) , aStamp(nullptr) , aAddToQuickTools(nullptr) @@ -105,12 +107,14 @@ public: PageViewAnnotator *annotator; + QList quickTools; QList textTools; QList textQuickTools; QActionGroup *agTools; QAction *agLastAction; ToggleActionMenu *aQuickTools; + ActionBar *aQuickToolsBar; ToggleActionMenu *aGeomShapes; ToggleActionMenu *aStamp; QAction *aAddToQuickTools; @@ -351,20 +355,19 @@ void AnnotationActionHandlerPrivate::populateQuickAnnotations() q->deselectAllAnnotationActions(); } - const QList quickToolActions = aQuickTools->menu()->actions(); - for (QAction *action : quickToolActions) { - if (action->isCheckable()) { - aQuickTools->removeAction(action); - delete action; - } + for (QAction *action : qAsConst(quickTools)) { + aQuickTools->removeAction(action); + aQuickToolsBar->removeAction(action); + delete action; } - QAction *aSeparator = aQuickTools->menu()->actions().first(); + quickTools.clear(); textQuickTools.clear(); int favToolId = 1; QList::const_iterator shortcutNumber = numberKeys.begin(); QDomElement favToolElement = annotator->quickTool(favToolId); - QList quickTools; + int actionBarInsertPosition = 0; + QAction *aSeparator = aQuickTools->menu()->actions().first(); while (!favToolElement.isNull()) { QString itemText = favToolElement.attribute(QStringLiteral("name")); if (favToolElement.attribute(QStringLiteral("default"), QStringLiteral("false")) == QLatin1String("true")) { @@ -376,6 +379,7 @@ void AnnotationActionHandlerPrivate::populateQuickAnnotations() QIcon toolIcon = QIcon(PageViewAnnotator::makeToolPixmap(favToolElement)); QAction *annFav = new KToggleAction(toolIcon, itemText, q); aQuickTools->insertAction(aSeparator, annFav); + aQuickToolsBar->insertAction(actionBarInsertPosition++, annFav); agTools->addAction(annFav); quickTools.append(annFav); if (shortcutNumber != numberKeys.end()) @@ -392,6 +396,7 @@ void AnnotationActionHandlerPrivate::populateQuickAnnotations() } favToolElement = annotator->quickTool(++favToolId); } + aQuickToolsBar->recreateWidgets(); // set the default action if (quickTools.isEmpty()) { @@ -518,7 +523,7 @@ void AnnotationActionHandlerPrivate::slotToolBarVisibilityChanged(bool checked) bool AnnotationActionHandlerPrivate::isQuickToolAction(QAction *aTool) { - return aQuickTools->menu()->actions().contains(aTool) && aTool->isCheckable(); + return quickTools.contains(aTool); } bool AnnotationActionHandlerPrivate::isQuickToolStamp(int toolId) @@ -650,6 +655,10 @@ AnnotationActionHandler::AnnotationActionHandler(PageViewAnnotator *parent, KAct d->aQuickTools->setDefaultAction(action); } }); + + d->aQuickToolsBar = new ActionBar(this); + d->aQuickToolsBar->setText(i18n("Quick Annotation Bar")); + QAction *aQuickToolsSeparator = new QAction(this); aQuickToolsSeparator->setSeparator(true); d->aQuickTools->addAction(aQuickToolsSeparator); @@ -657,6 +666,7 @@ AnnotationActionHandler::AnnotationActionHandler(PageViewAnnotator *parent, KAct QAction *aConfigAnnotation = ac->action(QStringLiteral("options_configure_annotations")); if (aConfigAnnotation) { d->aQuickTools->addAction(aConfigAnnotation); + d->aQuickToolsBar->addAction(aConfigAnnotation); } d->populateQuickAnnotations(); @@ -721,6 +731,7 @@ AnnotationActionHandler::AnnotationActionHandler(PageViewAnnotator *parent, KAct ac->addAction(QStringLiteral("mouse_toggle_annotate"), d->aToolBarVisibility); ac->addAction(QStringLiteral("hide_annotation_toolbar"), d->aHideToolBar); + ac->addAction(QStringLiteral("quick_annotation_action_bar"), d->aQuickToolsBar); ac->addAction(QStringLiteral("annotation_highlighter"), aHighlighter); ac->addAction(QStringLiteral("annotation_underline"), aUnderline); ac->addAction(QStringLiteral("annotation_squiggle"), aSquiggle);