File indexing completed on 2024-09-29 10:43:08
0001 /* 0002 SPDX-FileCopyrightText: 2020 Michail Vourlakos <mvourlakos@gmail.com> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "genericdialog.h" 0007 0008 // Qt 0009 #include <QDebug> 0010 #include <QFileDialog> 0011 #include <QKeyEvent> 0012 #include <QPushButton> 0013 #include <QVBoxLayout> 0014 0015 // KDE 0016 #include <KLocalizedString> 0017 #include <KStandardGuiItem> 0018 0019 static const int ERRORINTERVAL = 4000; 0020 static const int INFORMATIONINTERVAL = 3000; 0021 static const int INFORMATIONWITHACTIONINTERVAL = 5000; 0022 static const int POSITIVEINTERVAL = 3000; 0023 static const int WARNINGINTERVAL = 3500; 0024 0025 namespace Latte { 0026 namespace Settings { 0027 namespace Dialog { 0028 0029 GenericDialog::GenericDialog(QWidget *parent, Qt::WindowFlags f) 0030 : QDialog(parent, f) 0031 { 0032 } 0033 0034 GenericDialog::~GenericDialog() 0035 { 0036 } 0037 0038 KMessageWidget *GenericDialog::initMessageWidget() 0039 { 0040 QVBoxLayout *vLayout = qobject_cast<QVBoxLayout *>(layout()); 0041 0042 if (!vLayout) { 0043 return nullptr; 0044 } 0045 0046 auto messagewidget = new KMessageWidget(this); 0047 vLayout->insertWidget(vLayout->count()-1, messagewidget); 0048 connect(messagewidget, &KMessageWidget::hideAnimationFinished, messagewidget, &QObject::deleteLater); 0049 0050 return messagewidget; 0051 } 0052 0053 void GenericDialog::deleteInlineMessages() 0054 { 0055 //QVBoxLayout *vlayout = qobject_cast<QVBoxLayout *>(layout()); 0056 0057 for (int i=0; i<children().count(); ++i) { 0058 QObject *child = children()[i]; 0059 KMessageWidget *messagewidget = qobject_cast<KMessageWidget *>(child); 0060 0061 if(messagewidget) { 0062 delete messagewidget; 0063 } 0064 } 0065 } 0066 0067 KMessageBox::ButtonCode GenericDialog::saveChangesConfirmation(const QString &text) 0068 { 0069 QString dialogtext = text.isEmpty() ? i18n("The settings have changed.<br/>Do you want to apply the changes or discard them?") : text; 0070 0071 return KMessageBox::warningYesNoCancel(this, 0072 dialogtext, 0073 i18n("Apply Settings"), 0074 KStandardGuiItem::apply(), 0075 KStandardGuiItem::discard()); 0076 } 0077 0078 void GenericDialog::showInlineMessage(const QString &msg, const KMessageWidget::MessageType &type, const bool &isPersistent, QList<QAction *> actions) 0079 { 0080 if (msg.isEmpty()) { 0081 return; 0082 } 0083 0084 auto messagewidget = initMessageWidget(); 0085 0086 if (!messagewidget) { 0087 return; 0088 } 0089 0090 int hideInterval = 0; 0091 0092 if (!isPersistent) { 0093 if (type == KMessageWidget::Error) { 0094 hideInterval = ERRORINTERVAL; 0095 } else if (type == KMessageWidget::Warning) { 0096 hideInterval = WARNINGINTERVAL; 0097 } else if (type == KMessageWidget::Information) { 0098 hideInterval = (actions.count() == 0 ? INFORMATIONINTERVAL : INFORMATIONWITHACTIONINTERVAL); 0099 } else if (type == KMessageWidget::Positive) { 0100 hideInterval = POSITIVEINTERVAL; 0101 } 0102 } 0103 0104 messagewidget->setCloseButtonVisible(actions.count()==0); 0105 0106 if (actions.count() > 0) { 0107 QAction *cancelaction = new QAction(i18n("Hide"), this); 0108 cancelaction->setIcon(QIcon::fromTheme("dialog-cancel")); 0109 actions << cancelaction; 0110 } 0111 0112 for (int i=0; i<actions.count(); ++i) { 0113 connect(actions[i], &QAction::triggered, messagewidget, &KMessageWidget::animatedHide); 0114 messagewidget->addAction(actions[i]); 0115 } 0116 0117 messagewidget->setText(msg); 0118 0119 // TODO: wrap at arbitrary character positions once QLabel can do this 0120 // https://bugreports.qt.io/browse/QTBUG-1276 0121 messagewidget->setWordWrap(true); 0122 messagewidget->setMessageType(type); 0123 messagewidget->setWordWrap(false); 0124 0125 const int unwrappedWidth = messagewidget->sizeHint().width(); 0126 messagewidget->setWordWrap(unwrappedWidth > size().width()); 0127 0128 // for some reason this is not smoooth so it is disabled 0129 //messagewidget->animatedShow(); 0130 0131 if (hideInterval > 0) { 0132 QTimer *hidetimer = new QTimer(messagewidget); 0133 hidetimer->setInterval(hideInterval); 0134 0135 connect(hidetimer, &QTimer::timeout, this, [&, messagewidget]() { 0136 messagewidget->animatedHide(); 0137 }); 0138 0139 hidetimer->start(); 0140 } 0141 } 0142 0143 } 0144 } 0145 }