File: pagedialog.cpp

package info (click to toggle)
ktikz 0.13.1%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,136 kB
  • sloc: cpp: 10,491; xml: 701; sh: 131; makefile: 19
file content (159 lines) | stat: -rw-r--r-- 5,989 bytes parent folder | download | duplicates (4)
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
/***************************************************************************
 *   Copyright (C) 2007, 2008, 2009, 2011 by Glad Deschrijver              *
 *     <glad.deschrijver@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; if not, see <http://www.gnu.org/licenses/>.  *
 ***************************************************************************/

#include "pagedialog.h"
#ifdef KTIKZ_USE_KDE
#include <QPushButton>

PageDialog::PageDialog(QWidget *parent) : KPageDialog(parent)
{
	setFaceType(List);
	setStandardButtons(QDialogButtonBox::Ok|QDialogButtonBox::Cancel|QDialogButtonBox::Help|QDialogButtonBox::Apply);
	QPushButton *okButton = buttonBox()->button(QDialogButtonBox::Ok);
	okButton->setDefault(true);
	okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
	connect(buttonBox ()->button(QDialogButtonBox::Apply), SIGNAL(clicked()), this, SLOT(accept()));
	connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
}

void PageDialog::addPage(QWidget *widget, const QString &title, const QString &iconName)
{
	QString titleString = title;
	titleString.remove(QLatin1Char('&'));

	KPageWidgetItem *page = new KPageWidgetItem(widget, titleString);
	page->setHeader(titleString);
	page->setIcon(QIcon::fromTheme(iconName));
	KPageDialog::addPage(page);
}
#else
#include <QAction>
#include <QApplication>
#include <QDialogButtonBox>
#include <QGridLayout>
#include <QLabel>
#include <QListWidget>
#include <QStackedWidget>
#include <QToolButton>
#include <QWhatsThis>

#include <QDialogButtonBox>
#include <QPushButton>
#include <QVBoxLayout>

#include "icon.h"

PageDialog::PageDialog(QWidget *parent) : QDialog(parent)
{
	m_iconWidth = 0;

	// add What's this, OK, Cancel buttons
	QDialogButtonBox *buttonBox = new QDialogButtonBox;
	QAction *whatsThisAction = QWhatsThis::createAction(this);
	whatsThisAction->setIcon(Icon(QLatin1String("help-contextual")));
	QToolButton *whatsThisButton = new QToolButton(this);
	whatsThisButton->setDefaultAction(whatsThisAction);
	whatsThisButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Ignored);
	buttonBox->addButton(whatsThisButton, QDialogButtonBox::HelpRole);
	buttonBox->addButton(QDialogButtonBox::Ok);
	buttonBox->addButton(QDialogButtonBox::Cancel);
	connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
	connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));

	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->addWidget(centerWidget());
	mainLayout->addWidget(buttonBox);
	setLayout(mainLayout);
}

QWidget *PageDialog::centerWidget()
{
	// create list
	m_pagesListWidget = new QListWidget;
	m_pagesListWidget->setViewMode(QListView::IconMode);
	m_pagesListWidget->setMovement(QListView::Static);
	m_pagesListWidget->setFlow(QListView::TopToBottom);
	m_pagesListWidget->setWordWrap(true);
	m_pagesListWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);

	// create title
	QFrame *titleFrame = new QFrame(this);
	titleFrame->setFrameShape(QFrame::Box);
	m_pagesTitleLabel = new QLabel(titleFrame);
	m_pagesTitleLabel->setStyleSheet(QLatin1String("QLabel { font-weight: bold; }"));
	QGridLayout *titleLayout = new QGridLayout(titleFrame);
	titleLayout->setColumnStretch(0, 1);
	titleLayout->setMargin(6);
	titleLayout->addWidget(m_pagesTitleLabel);

	// add pages
	m_pagesStackedWidget = new QStackedWidget;
	connect(m_pagesListWidget, SIGNAL(currentRowChanged(int)), m_pagesStackedWidget, SLOT(setCurrentIndex(int)));
	connect(m_pagesListWidget, SIGNAL(currentRowChanged(int)), this, SLOT(setCurrentPage(int)));

	QWidget *mainWidget = new QWidget;
	QGridLayout *mainLayout = new QGridLayout;
	mainLayout->addWidget(m_pagesListWidget, 0, 0, 2, 1);
	mainLayout->addWidget(titleFrame, 0, 1);
	mainLayout->addWidget(m_pagesStackedWidget, 1, 1);
	mainWidget->setLayout(mainLayout);

	return mainWidget;
}

void PageDialog::setCaption(const QString &caption)
{
	setWindowTitle(caption);
}

void PageDialog::setHelp(const QString &anchor)
{
	Q_UNUSED(anchor);
	// not used in Qt-only version
}

void PageDialog::addPage(QWidget *widget, const QString &title, const QString &iconName)
{
	QString titleString = title;
	titleString.remove(QLatin1Char('&'));

	QListWidgetItem *item = new QListWidgetItem(Icon(iconName), titleString);
	item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
	item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
	m_pagesListWidget->addItem(item);
	m_pagesListWidgetItems << item;

	m_iconWidth = qMax(m_iconWidth, m_pagesListWidget->visualItemRect(item).width() + 6);
	m_pagesListWidget->setFixedWidth(m_iconWidth);
	m_pagesListWidget->setCurrentRow(0);

	// center all labels and icons
	for (int i = 0; i < m_pagesListWidget->count(); ++i)
	{
		QListWidgetItem *item = m_pagesListWidget->item(i);
		item->setSizeHint(QSize(m_pagesListWidget->sizeHintForColumn(0), m_pagesListWidget->visualItemRect(item).height()));
	}

	m_pagesStackedWidget->addWidget(widget);
}

void PageDialog::setCurrentPage(int page)
{
	m_pagesTitleLabel->setText(m_pagesListWidgetItems.at(page)->text());
}
#endif