Commit 8837a113 authored by Simone Gaiarin's avatar Simone Gaiarin
Browse files

Add quick annotation minibar action

BUG: 425438
BUG: 425598

Fix #20
parent a3f242cb
......@@ -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
......
/***************************************************************************
* Copyright (C) 2020 by Simone Gaiarin <simgunz@gmail.com> *
* *
* 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 <QAction>
#include <QHBoxLayout>
#include <QLabel>
#include <QLayout>
#include <QList>
#include <QToolBar>
#include <QToolButton>
#include <QVBoxLayout>
#include <QWidget>
#include "actionbar.h"
class ActionBarWidget : public QWidget
{
Q_OBJECT
public:
ActionBarWidget(QToolBar *parent);
void recreateButtons(const QList<QAction *> &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<QAction *> &actions)
{
QToolBar *parentToolbar = qobject_cast<QToolBar *>(parentWidget());
if (!parentToolbar) {
return;
}
for (auto &toolButton : findChildren<QToolButton *>()) {
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<QToolButton *>()) {
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<QToolBar *>(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<ActionBarWidget *>(widget);
if (actionBarWidget) {
actionBarWidget->recreateButtons(m_actions);
}
}
}
#include "actionbar.moc"
/***************************************************************************
* Copyright (C) 2020 by Simone Gaiarin <simgunz@gmail.com> *
* *
* 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 <QWidgetAction>
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<QAction *> m_actions;
};
#endif
......@@ -25,6 +25,7 @@
#include <kwidgetsaddons_version.h>
// 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<QAction *> quickTools;
QList<QAction *> textTools;
QList<QAction *> 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<QAction *> 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<int>::const_iterator shortcutNumber = numberKeys.begin();
QDomElement favToolElement = annotator->quickTool(favToolId);
QList<QAction *> 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);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment