1 // vim: set tabstop=4 shiftwidth=4 expandtab:
2 /*
3 Gwenview: an image viewer
4 Copyright 2014 Aurélien Gâteau <agateau@kde.org>
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 // Self
20 #include <hud/hudbuttonbox.h>
21
22 // Local
23 #include <graphicswidgetfloater.h>
24 #include <hud/hudbutton.h>
25 #include <hud/hudcountdown.h>
26 #include <hud/hudlabel.h>
27
28 // KF
29
30 // Qt
31 #include <QAction>
32 #include <QGraphicsLinearLayout>
33 #include <QGraphicsWidget>
34
35 namespace Gwenview
36 {
37 struct HudButtonBoxPrivate {
38 QGraphicsLinearLayout *mLayout;
39 HudLabel *mLabel;
40 QList<HudButton *> mButtonList;
41 HudCountDown *mCountDown;
42
updateButtonWidthsGwenview::HudButtonBoxPrivate43 void updateButtonWidths()
44 {
45 qreal minWidth = 0;
46 for (HudButton *button : qAsConst(mButtonList)) {
47 minWidth = qMax(minWidth, button->preferredWidth());
48 }
49 for (HudButton *button : qAsConst(mButtonList)) {
50 button->setMinimumWidth(minWidth);
51 }
52 }
53 };
54
HudButtonBox(QGraphicsWidget * parent)55 HudButtonBox::HudButtonBox(QGraphicsWidget *parent)
56 : HudWidget(parent)
57 , d(new HudButtonBoxPrivate)
58 {
59 d->mCountDown = nullptr;
60 auto *content = new QGraphicsWidget();
61 d->mLayout = new QGraphicsLinearLayout(Qt::Vertical, content);
62 d->mLabel = new HudLabel();
63 d->mLayout->addItem(d->mLabel);
64 d->mLayout->setItemSpacing(0, 24);
65 init(content, HudWidget::OptionNone);
66
67 setContentsMargins(6, 6, 6, 6);
68 setAutoDeleteOnFadeout(true);
69 }
70
~HudButtonBox()71 HudButtonBox::~HudButtonBox()
72 {
73 delete d;
74 }
75
addCountDown(qreal ms)76 void HudButtonBox::addCountDown(qreal ms)
77 {
78 Q_ASSERT(!d->mCountDown);
79 d->mCountDown = new HudCountDown(this);
80 connect(d->mCountDown, &HudCountDown::timeout, this, &HudButtonBox::fadeOut);
81
82 auto *floater = new GraphicsWidgetFloater(this);
83 floater->setChildWidget(d->mCountDown);
84 floater->setAlignment(Qt::AlignRight | Qt::AlignBottom);
85 floater->setHorizontalMargin(6);
86 floater->setVerticalMargin(6);
87
88 d->mCountDown->start(ms);
89 }
90
addAction(QAction * action,const QString & overrideText)91 HudButton *HudButtonBox::addAction(QAction *action, const QString &overrideText)
92 {
93 QString text = overrideText.isEmpty() ? action->text() : overrideText;
94 HudButton *button = addButton(text);
95 connect(button, &HudButton::clicked, action, &QAction::trigger);
96 return button;
97 }
98
addButton(const QString & text)99 HudButton *HudButtonBox::addButton(const QString &text)
100 {
101 auto *button = new HudButton();
102 connect(button, &HudButton::clicked, this, &HudButtonBox::fadeOut);
103 button->setText(text);
104 d->mLayout->addItem(button);
105 d->mLayout->setAlignment(button, Qt::AlignCenter);
106 d->mButtonList += button;
107
108 return button;
109 }
110
setText(const QString & msg)111 void HudButtonBox::setText(const QString &msg)
112 {
113 d->mLabel->setText(msg);
114 }
115
showEvent(QShowEvent * event)116 void HudButtonBox::showEvent(QShowEvent *event)
117 {
118 HudWidget::showEvent(event);
119 d->updateButtonWidths();
120 }
121
122 } // namespace
123