1 /*****************************************************************************
2  *   Copyright 2015 - 2015 Yichao Yu <yyc1992@gmail.com>                     *
3  *                                                                           *
4  *   This program is free software; you can redistribute it and/or modify    *
5  *   it under the terms of the GNU Lesser General Public License as          *
6  *   published by the Free Software Foundation; either version 2.1 of the    *
7  *   License, or (at your option) version 3, or any later version accepted   *
8  *   by the membership of KDE e.V. (or its successor approved by the         *
9  *   membership of KDE e.V.), which shall act as a proxy defined in          *
10  *   Section 6 of version 3 of the license.                                  *
11  *                                                                           *
12  *   This program is distributed in the hope that it will be useful,         *
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       *
15  *   Lesser General Public License for more details.                         *
16  *                                                                           *
17  *   You should have received a copy of the GNU Lesser General Public        *
18  *   License along with this library. If not,                                *
19  *   see <http://www.gnu.org/licenses/>.                                     *
20  *****************************************************************************/
21 
22 #ifndef __COMMON_KF5_UTILS_H__
23 #define __COMMON_KF5_UTILS_H__
24 
25 #include <qtcurve-utils/utils.h>
26 
27 #include <kiconengine.h>
28 #include <kiconloader.h>
29 
30 #include <QStandardPaths>
31 #include <QDir>
32 #include <QDialog>
33 
34 class QDialogButtonBox;
35 class QValidator;
36 class QLineEdit;
37 class QLabel;
38 
39 namespace QtCurve {
40 
41 QDialogButtonBox *createDialogButtonBox(QDialog *dialog);
42 
43 class InputDialog : public QDialog {
44     Q_OBJECT
45 public:
46     explicit InputDialog(QWidget *parent=nullptr, Qt::WindowFlags=0);
47 
48     void setTitle(const QString &title);
49     void setLabelText(const QString &label);
50     void setText(const QString &text);
51     void setValidator(QValidator *validator);
52 
53     QString getLabelText();
54     QString getText();
55 
56     static QString getText(QWidget *parent, const QString &title,
57                            const QString &label, const QString &text,
58                            QValidator *validator=nullptr,
59                            bool *ok=nullptr, Qt::WindowFlags flags=0);
60 
61 private:
62     void checkValid(const QString &text);
63 
64 private:
65     QLabel *m_label;
66     QLineEdit *m_text;
67     QDialogButtonBox *m_buttonBox;
68     QValidator *m_validator;
69 };
70 
71 static inline QIcon
loadKIcon(const QString & name)72 loadKIcon(const QString &name)
73 {
74     return QIcon(new KIconEngine(name, KIconLoader::global()));
75 }
76 
77 // TODO probably merge with utils/dirs
78 static inline QString
saveLocation(QStandardPaths::StandardLocation type,const char * suffix)79 saveLocation(QStandardPaths::StandardLocation type, const char *suffix)
80 {
81     QString path = QStandardPaths::writableLocation(type);
82     QTC_RET_IF_FAIL(!path.isEmpty(), path);
83     path += '/' + QString::fromUtf8(suffix);
84     QDir().mkpath(path);
85     return path;
86 }
87 
88 static inline QString
qtcSaveDir()89 qtcSaveDir()
90 {
91     return saveLocation(QStandardPaths::GenericDataLocation, "QtCurve/");
92 }
93 
94 }
95 
96 #endif
97