File: configdialog.cpp

package info (click to toggle)
cantata 3.4.0.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,428 kB
  • sloc: cpp: 109,584; perl: 1,366; xml: 722; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (466 lines) | stat: -rw-r--r-- 11,984 bytes parent folder | download
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
 * Cantata
 *
 * Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
 *
 * ----
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "configdialog.h"
#include "utils.h"
#ifdef __APPLE__
#include "icon.h"
#include "osxstyle.h"
#include "windowmanager.h"
#include <QAbstractItemView>
#include <QBoxLayout>
#include <QButtonGroup>
#include <QColor>
#include <QComboBox>
#include <QDialogButtonBox>
#include <QKeyEvent>
#include <QLineEdit>
#include <QLinearGradient>
#include <QOperatingSystemVersion>
#include <QPainter>
#include <QPointF>
#include <QPropertyAnimation>
#include <QPushButton>
#include <QRect>
#include <QScreen>
#include <QStackedWidget>
#include <QStyleOptionToolButton>
#include <QStylePainter>
#include <QTextEdit>
#include <QTimer>
#include <QToolBar>
#include <QToolButton>
#else
#include "pagewidget.h"
#endif

#ifdef __APPLE__
//#define ANIMATE_RESIZE
static void drawFadedLine(QPainter* p, const QRect& r, const QColor& col, bool horiz, double fadeSize)
{
	QPointF start(r.x() + 0.5, r.y() + 0.5);
	QPointF end(r.x() + (horiz ? r.width() - 1 : 0) + 0.5,
	            r.y() + (horiz ? 0 : r.height() - 1) + 0.5);
	QLinearGradient grad(start, end);
	QColor fade(col);

	fade.setAlphaF(0.0);
	grad.setColorAt(0, fade);
	grad.setColorAt(fadeSize, col);
	grad.setColorAt(1.0 - fadeSize, col);
	grad.setColorAt(1, fade);
	p->setPen(QPen(QBrush(grad), 1));
	p->drawLine(start, end);
}

class ConfigDialogToolButton : public QToolButton {
public:
	ConfigDialogToolButton(QWidget* p)
		: QToolButton(p)
	{
		setStyleSheet("QToolButton {border: 0}");
		setMinimumWidth(56);
	}

	void paintEvent(QPaintEvent* e)
	{
		Q_UNUSED(e)
		if (isChecked()) {
			QPainter p(this);
			QColor col(Qt::black);
			QRect r(rect());

			if (QOperatingSystemVersion::current() >= QOperatingSystemVersion(QOperatingSystemVersion::MacOS, 10, 9)) {
				col.setAlphaF(0.1);
				p.setClipRect(r);
				p.setRenderHint(QPainter::Antialiasing, true);
				p.fillPath(Utils::buildPath(r.adjusted(0, 0, 0, 32), 4), col);
			}
			else {
				QLinearGradient bgndGrad(r.topLeft(), r.bottomLeft());
				col.setAlphaF(0.01);
				bgndGrad.setColorAt(0, col);
				bgndGrad.setColorAt(1, col);
				col.setAlphaF(0.1);
				bgndGrad.setColorAt(0.25, col);
				bgndGrad.setColorAt(0.75, col);
				p.fillRect(r, bgndGrad);
				p.setRenderHint(QPainter::Antialiasing, true);
				col.setAlphaF(0.25);
				drawFadedLine(&p, QRect(r.x(), r.y(), r.x(), r.y() + r.height() - 1), col, false, 0.25);
				drawFadedLine(&p, QRect(r.x() + r.width() - 1, r.y(), r.x() + 1, r.y() + r.height() - 1), col, false, 0.25);
			}
		}
		QStylePainter p(this);
		QStyleOptionToolButton opt;
		initStyleOption(&opt);
		if (isDown()) {
			opt.state &= QStyle::State_Sunken;
		}
		p.drawComplexControl(QStyle::CC_ToolButton, opt);
	}
};

static const int constMinPad = 16;
#endif

ConfigDialog::ConfigDialog(QWidget* parent, const QString& name, const QSize& defSize, bool instantApply)
#ifdef __APPLE__
	: QMainWindow(parent), shown(false), resizeAnim(0)
#else
	: Dialog(parent, name, defSize)
#endif
{
	instantApply = false;// TODO!!!!

#ifdef __APPLE__
	Q_UNUSED(defSize)
	Q_UNUSED(name)

	setUnifiedTitleAndToolBarOnMac(true);
	toolBar = addToolBar("ToolBar");
	toolBar->setMovable(false);
	toolBar->setContextMenuPolicy(Qt::PreventContextMenu);
	toolBar->setStyleSheet("QToolBar { spacing:0px} ");
	QWidget* left = new QWidget(toolBar);
	left->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	toolBar->addWidget(left);
	QWidget* right = new QWidget(toolBar);
	right->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	rightSpacer = toolBar->addWidget(right);
	left->setMinimumWidth(constMinPad);
	right->setMinimumWidth(constMinPad);
	QWidget* mw = new QWidget(this);
	QBoxLayout* lay = new QBoxLayout(QBoxLayout::TopToBottom, mw);
	stack = new QStackedWidget(mw);
	group = new QButtonGroup(toolBar);
	lay->addWidget(stack);
	if (instantApply) {
		buttonBox = 0;
		setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint);
	}
	else {
		buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Apply, Qt::Horizontal, mw);
		lay->addWidget(buttonBox);
		connect(buttonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(macButtonPressed(QAbstractButton*)));
		buttonBox->setStyle(Dialog::buttonProxyStyle());
		// Hide window buttons if not instany apply - dont want user just closing dialog
		setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint | Qt::WindowMaximizeButtonHint);
	}

	group->setExclusive(true);
	setCentralWidget(mw);
	WindowManager* wm = new WindowManager(toolBar);
	wm->initialize(WindowManager::WM_DRAG_MENU_AND_TOOLBAR);
	wm->registerWidgetAndChildren(toolBar);
#else

	bool limitedHeight = Utils::limitedHeight(this);
	pageWidget = new PageWidget(this, limitedHeight, !limitedHeight);
	setMainWidget(pageWidget);
	if (instantApply) {
		// TODO: What???
	}
	else {
		setButtons(Dialog::Ok | Dialog::Cancel | Dialog::Apply);
	}
#endif
}

ConfigDialog::~ConfigDialog()
{
#ifdef __APPLE__
	OSXStyle::self()->removeWindow(this);
#endif
}

void ConfigDialog::addPage(const QString& id, QWidget* widget, const QString& name, const QIcon& icon, const QString& header)
{
	Q_UNUSED(header)
#ifdef __APPLE__

	Page newPage;
	newPage.item = new ConfigDialogToolButton(this);
	newPage.item->setText(name);
	newPage.item->setIcon(icon);
	newPage.item->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
	newPage.item->setCheckable(true);
	newPage.item->setChecked(false);
	newPage.item->setProperty("id", id);
	newPage.item->ensurePolished();
	toolBar->insertWidget(rightSpacer, newPage.item);
	newPage.widget = widget;
	newPage.index = stack->count();
	stack->addWidget(widget);
	group->addButton(newPage.item);
	pages.insert(id, newPage);
	connect(newPage.item, SIGNAL(toggled(bool)), this, SLOT(activatePage()));
	int sz = 0;
	QMap<QString, Page>::const_iterator it = pages.begin();
	QMap<QString, Page>::const_iterator end = pages.end();
	for (; it != end; ++it) {
		sz += it.value().item->sizeHint().width() + 4;
	}
	setMinimumWidth(sz + (2 * constMinPad) + 48);
#else

	pages.insert(id, pageWidget->addPage(widget, name, icon, header));

#endif
}

bool ConfigDialog::setCurrentPage(const QString& id)
{
	if (!pages.contains(id)) {
		return false;
	}

#ifdef __APPLE__
	QMap<QString, Page>::const_iterator it = pages.find(id);
	if (it != pages.end()) {
		stack->setCurrentIndex(it.value().index);
		setCaption(it.value().item->text());
		it.value().item->setChecked(true);
	}
#ifdef ANIMATE_RESIZE
	if (isVisible()) {
		it.value().widget->ensurePolished();
		ensurePolished();
	}
	int newH = buttonBox
			? (it.value().widget->sizeHint().height() + toolBar->height() + buttonBox->height() + layout()->spacing() + (2 * layout()->margin()))
			: (it.value().widget->sizeHint().height() + toolBar->height() + (2 * layout()->margin()));

	if (isVisible()) {
		if (newH != height()) {
			if (!resizeAnim) {
				resizeAnim = new QPropertyAnimation(this, "h");
				resizeAnim->setDuration(250);
			}
			resizeAnim->setStartValue(height());
			resizeAnim->setEndValue(newH);
			resizeAnim->start();
		}
	}
	else {
		setFixedHeight(newH);
		resize(width(), newH);
		setMaximumHeight(QWIDGETSIZE_MAX);
	}
#endif
#else
	pageWidget->setCurrentPage(pages[id]);
#endif
	return true;
}

#ifdef __APPLE__
void ConfigDialog::setH(int h)
{
	if (resizeAnim && h == resizeAnim->endValue().toInt()) {
		setMaximumHeight(QWIDGETSIZE_MAX);
		resize(width(), h);
	}
	else {
		setFixedHeight(h);
	}
}
#endif

QWidget* ConfigDialog::getPage(const QString& id) const
{
	if (!pages.contains(id)) {
		return nullptr;
	}

#ifdef __APPLE__
	return pages[id].widget;
#else
	return pages[id]->widget();
#endif
}

void ConfigDialog::slotButtonClicked(int button)
{
	switch (button) {
	case Dialog::Ok:
	case Dialog::Apply:
		save();
		break;
	case Dialog::Cancel:
		cancel();
		reject();
#ifndef __APPLE__
		// Need to call this - if not, when dialog is closed by window X control, it is not deleted!!!!
		Dialog::slotButtonClicked(button);
#endif
		break;
	default:
		break;
	}

	if (Dialog::Ok == button) {
		accept();
	}

#ifndef __APPLE__
	Dialog::slotButtonClicked(button);
#endif
}

void ConfigDialog::activatePage()
{
#ifdef __APPLE__
	QToolButton* item = qobject_cast<QToolButton*>(sender());
	if (item && item->isChecked()) {
		setCurrentPage(item->property("id").toString());
	}
#endif
}

#ifdef __APPLE__
void ConfigDialog::accept()
{
	close();
}

void ConfigDialog::reject()
{
	close();
}

void ConfigDialog::keyPressEvent(QKeyEvent* e)
{
	// Only act on Esc if we are instant-apply (in which case there will be no button box)...
	if (!buttonBox && Qt::Key_Escape == e->key() && Qt::NoModifier == e->modifiers()) {
		close();
	}
}

void ConfigDialog::hideEvent(QHideEvent* e)
{
	OSXStyle::self()->removeWindow(this);
	QMainWindow::hideEvent(e);
}

void ConfigDialog::closeEvent(QCloseEvent* e)
{
	OSXStyle::self()->removeWindow(this);
	QMainWindow::closeEvent(e);
}

static bool isFocusable(QWidget* w)
{
	if (/*qobject_cast<QAbstractButton *>(w) || qobject_cast<QComboBox *>(w) ||*/ qobject_cast<QLineEdit*>(w) || qobject_cast<QTextEdit*>(w) || qobject_cast<QAbstractItemView*>(w)) {
		if (w->isVisible() && w->isEnabled()) {
			return true;
		}
	}
	return false;
}

static QWidget* firstFocusableWidget(QWidget* w)
{
	if (!w) {
		return 0;
	}
	if (isFocusable(w)) {
		return w;
	}

	QObjectList children = w->children();
	// Try each child first...
	for (QObject* c : children) {
		if (qobject_cast<QWidget*>(c)) {
			QWidget* cw = static_cast<QWidget*>(c);
			if (isFocusable(cw)) {
				return cw;
			}
		}
	}
	// Now try grandchildren...
	for (QObject* c : children) {
		if (qobject_cast<QWidget*>(c)) {
			QWidget* f = firstFocusableWidget(static_cast<QWidget*>(c));
			if (f) {
				return f;
			}
		}
	}
	return 0;
}

void ConfigDialog::showEvent(QShowEvent* e)
{
	if (!shown) {
		QTimer::singleShot(0, this, SLOT(setFocus()));
		shown = true;
		ensurePolished();

		QScreen* sc = QApplication::primaryScreen();
		if (sc) {
			move(QPoint((sc->availableGeometry().size().width() - width()) / 2, 86));
		}
	}
	OSXStyle::self()->addWindow(this);
	QMainWindow::showEvent(e);
	if (parentWidget()) {
		move(pos().x(), parentWidget()->pos().y() + 16);
	}
}
#endif

void ConfigDialog::setFocus()
{
#ifdef __APPLE__
	// When the pref dialog is shown, no widget has focus - and there is a small (4px x 4px?) upper left
	// corner of a focus highlight drawn in the upper left of the dialog.
	// To work-around this, after the dialog is shown, look for a widget that can be set to have focus...
	QWidget* w = firstFocusableWidget(stack->currentWidget());
	if (w) {
		w->setFocus();
	}
#endif
}

void ConfigDialog::macButtonPressed(QAbstractButton* b)
{
#ifdef __APPLE__
	if (!buttonBox) {
		return;
	}
	if (b == buttonBox->button(QDialogButtonBox::Ok)) {
		slotButtonClicked(Dialog::Ok);
	}
	else if (b == buttonBox->button(QDialogButtonBox::Apply)) {
		slotButtonClicked(Dialog::Apply);
	}
	else if (b == buttonBox->button(QDialogButtonBox::Cancel)) {
		slotButtonClicked(Dialog::Cancel);
	}
#else
	Q_UNUSED(b)
#endif
}

#include "moc_configdialog.cpp"