File indexing completed on 2025-01-05 04:02:07

0001 // vim: set tabstop=4 shiftwidth=4 expandtab:
0002 /*
0003 Gwenview: an image viewer
0004 Copyright 2014 Aurélien Gâteau <agateau@kde.org>
0005 
0006 This program is free software; you can redistribute it and/or
0007 modify it under the terms of the GNU General Public License
0008 as published by the Free Software Foundation; either version 2
0009 of the License, or (at your option) any later version.
0010 
0011 This program is distributed in the hope that it will be useful,
0012 but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 GNU General Public License for more details.
0015 
0016 You should have received a copy of the GNU General Public License
0017 along with this program.  If not, see <http://www.gnu.org/licenses/>.
0018 */
0019 // Self
0020 #include <hud/hudbuttonbox.h>
0021 
0022 // Local
0023 #include <graphicswidgetfloater.h>
0024 #include <hud/hudbutton.h>
0025 #include <hud/hudcountdown.h>
0026 #include <hud/hudlabel.h>
0027 
0028 // KF
0029 
0030 // Qt
0031 #include <QAction>
0032 #include <QGraphicsLinearLayout>
0033 #include <QGraphicsWidget>
0034 
0035 namespace Gwenview
0036 {
0037 struct HudButtonBoxPrivate {
0038     QGraphicsLinearLayout *mLayout = nullptr;
0039     HudLabel *mLabel = nullptr;
0040     QList<HudButton *> mButtonList;
0041     HudCountDown *mCountDown = nullptr;
0042 
0043     void updateButtonWidths()
0044     {
0045         qreal minWidth = 0;
0046         for (HudButton *button : qAsConst(mButtonList)) {
0047             minWidth = qMax(minWidth, button->preferredWidth());
0048         }
0049         for (HudButton *button : qAsConst(mButtonList)) {
0050             button->setMinimumWidth(minWidth);
0051         }
0052     }
0053 };
0054 
0055 HudButtonBox::HudButtonBox(QGraphicsWidget *parent)
0056     : HudWidget(parent)
0057     , d(new HudButtonBoxPrivate)
0058 {
0059     d->mCountDown = nullptr;
0060     auto content = new QGraphicsWidget();
0061     d->mLayout = new QGraphicsLinearLayout(Qt::Vertical, content);
0062     d->mLabel = new HudLabel();
0063     d->mLayout->addItem(d->mLabel);
0064     d->mLayout->setItemSpacing(0, 24);
0065     init(content, HudWidget::OptionNone);
0066 
0067     setContentsMargins(6, 6, 6, 6);
0068     setAutoDeleteOnFadeout(true);
0069 }
0070 
0071 HudButtonBox::~HudButtonBox()
0072 {
0073     delete d;
0074 }
0075 
0076 void HudButtonBox::addCountDown(qreal ms)
0077 {
0078     Q_ASSERT(!d->mCountDown);
0079     d->mCountDown = new HudCountDown(this);
0080     connect(d->mCountDown, &HudCountDown::timeout, this, &HudButtonBox::fadeOut);
0081 
0082     auto floater = new GraphicsWidgetFloater(this);
0083     floater->setChildWidget(d->mCountDown);
0084     floater->setAlignment(Qt::AlignRight | Qt::AlignBottom);
0085     floater->setHorizontalMargin(6);
0086     floater->setVerticalMargin(6);
0087 
0088     d->mCountDown->start(ms);
0089 }
0090 
0091 HudButton *HudButtonBox::addAction(QAction *action, const QString &overrideText)
0092 {
0093     const QString text = overrideText.isEmpty() ? action->text() : overrideText;
0094     HudButton *button = addButton(text);
0095     connect(button, &HudButton::clicked, action, &QAction::trigger);
0096     return button;
0097 }
0098 
0099 HudButton *HudButtonBox::addButton(const QString &text)
0100 {
0101     auto button = new HudButton();
0102     connect(button, &HudButton::clicked, this, &HudButtonBox::fadeOut);
0103     button->setText(text);
0104     d->mLayout->addItem(button);
0105     d->mLayout->setAlignment(button, Qt::AlignCenter);
0106     d->mButtonList += button;
0107 
0108     return button;
0109 }
0110 
0111 void HudButtonBox::setText(const QString &msg)
0112 {
0113     d->mLabel->setText(msg);
0114 }
0115 
0116 void HudButtonBox::showEvent(QShowEvent *event)
0117 {
0118     HudWidget::showEvent(event);
0119     d->updateButtonWidths();
0120 }
0121 
0122 } // namespace
0123 
0124 #include "moc_hudbuttonbox.cpp"