forked from psemiletov/tea-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui_utils.cpp
208 lines (164 loc) · 5.21 KB
/
gui_utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
this code is Public Domain
*/
#include <QFileInfoList>
#include <QFileInfo>
#include <QLabel>
#include <QPushButton>
#include <QMenu>
#include <QStringList>
#include <QAction>
#include "gui_utils.h"
#include "utils.h"
void create_menu_from_list (QObject *handler,
QMenu *menu,
const QStringList &list,
const char *method
)
{
menu->setTearOffEnabled (true);
for (QList <QString>::const_iterator i = list.begin(); i != list.end(); ++i)
{
if (! i->startsWith ("#"))
{
QAction *act = menu->addAction (*i);
handler->connect (act, SIGNAL(triggered()), handler, method);
}
}
}
//uses dir name as menuitem, no recursion
void create_menu_from_themes (QObject *handler,
QMenu *menu,
const QString &dir,
const char *method
)
{
menu->setTearOffEnabled (true);
QDir d (dir);
QFileInfoList lst_fi = d.entryInfoList (QDir::NoDotAndDotDot | QDir::Dirs,
QDir::IgnoreCase | QDir::LocaleAware | QDir::Name);
for (QList <QFileInfo>::iterator fi = lst_fi.begin(); fi != lst_fi.end(); ++fi)
{
if (fi->isDir())
{
if (has_css_file (fi->absoluteFilePath()))
{
QAction *act = menu->addAction (fi->fileName());
act->setData (fi->filePath());
handler->connect (act, SIGNAL(triggered()), handler, method);
}
else
{
QMenu *mni_temp = menu->addMenu (fi->fileName());
create_menu_from_themes (handler, mni_temp, fi->filePath(), method);
}
}
}
}
void create_menu_from_dir (QObject *handler,
QMenu *menu,
const QString &dir,
const char *method
)
{
menu->setTearOffEnabled (true);
QDir d (dir);
if (! d.exists())
return;
QFileInfoList lst_fi = d.entryInfoList (QDir::NoDotAndDotDot | QDir::AllEntries,
QDir::DirsFirst | QDir::IgnoreCase |
QDir::LocaleAware | QDir::Name);
for (QList <QFileInfo>::iterator fi = lst_fi.begin(); fi != lst_fi.end(); ++fi)
{
if (fi->isDir())
{
QMenu *mni_temp = menu->addMenu (fi->fileName());
create_menu_from_dir (handler, mni_temp, fi->filePath(), method);
}
else
{
QAction *act = menu->addAction (fi->fileName());
act->setData (fi->filePath());
handler->connect (act, SIGNAL(triggered()), handler, method);
}
}
}
QImage image_scale_by (const QImage &source,
bool by_side,
int value,
Qt::TransformationMode mode)
{
if (source.isNull())
return source;
bool horisontal = (source.width() > source.height());
int width;
int height;
if (by_side)
{
width = value;
height = value;
}
else
{
width = get_value (source.width(), value);
height = get_value (source.height(), value);
}
if (horisontal)
return source.scaledToWidth (width, mode);
else
return source.scaledToHeight (height, mode);
}
QLineEdit* new_line_edit (QBoxLayout *layout, const QString &label, const QString &def_value)
{
QHBoxLayout *lt_h = new QHBoxLayout;
QLabel *l = new QLabel (label);
QLineEdit *r = new QLineEdit;
r->setText (def_value);
lt_h->insertWidget (-1, l, 0, Qt::AlignLeft);
lt_h->insertWidget (-1, r, 1, Qt::AlignLeft);
layout->addLayout (lt_h);
return r;
}
QSpinBox* new_spin_box (QBoxLayout *layout, const QString &label, int min, int max, int value, int step)
{
QHBoxLayout *lt_h = new QHBoxLayout;
QLabel *l = new QLabel (label);
QSpinBox *r = new QSpinBox;
r->setSingleStep (step);
r->setRange (min, max);
r->setValue (value);
lt_h->insertWidget (-1, l, 0, Qt::AlignLeft);
lt_h->insertWidget (-1, r, 1, Qt::AlignLeft);
layout->addLayout (lt_h, 1);
return r;
}
QComboBox* new_combobox (QBoxLayout *layout,
const QString &label,
const QStringList &items,
const QString &def_value)
{
QHBoxLayout *lt_h = new QHBoxLayout;
QLabel *l = new QLabel (label);
QComboBox *r = new QComboBox;
r->addItems (items);
r->setCurrentIndex (r->findText (def_value));
lt_h->insertWidget (-1, l, 0, Qt::AlignLeft);
lt_h->insertWidget (-1, r, 1, Qt::AlignLeft);
layout->addLayout (lt_h);
return r;
}
QComboBox* new_combobox (QBoxLayout *layout,
const QString &label,
const QStringList &items,
int index)
{
QHBoxLayout *lt_h = new QHBoxLayout;
QLabel *l = new QLabel (label);
QComboBox *r = new QComboBox;
r->addItems (items);
r->setCurrentIndex (index);
lt_h->insertWidget (-1, l, 0, Qt::AlignLeft);
lt_h->insertWidget (-1, r, 1, Qt::AlignLeft);
layout->addLayout (lt_h);
return r;
}