source: trunk/examples/widgets/charactermap/mainwindow.cpp@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 6.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include <QtGui>
42
43#include "characterwidget.h"
44#include "mainwindow.h"
45
46//! [0]
47MainWindow::MainWindow()
48{
49 QWidget *centralWidget = new QWidget;
50
51 QLabel *fontLabel = new QLabel(tr("Font:"));
52 fontCombo = new QFontComboBox;
53 QLabel *sizeLabel = new QLabel(tr("Size:"));
54 sizeCombo = new QComboBox;
55 QLabel *styleLabel = new QLabel(tr("Style:"));
56 styleCombo = new QComboBox;
57 QLabel *fontMergingLabel = new QLabel(tr("Automatic Font Merging:"));
58 fontMerging = new QCheckBox;
59 fontMerging->setChecked(true);
60
61 scrollArea = new QScrollArea;
62 characterWidget = new CharacterWidget;
63 scrollArea->setWidget(characterWidget);
64//! [0]
65
66//! [1]
67 findStyles(fontCombo->currentFont());
68//! [1]
69 findSizes(fontCombo->currentFont());
70
71//! [2]
72 lineEdit = new QLineEdit;
73 QPushButton *clipboardButton = new QPushButton(tr("&To clipboard"));
74//! [2]
75
76//! [3]
77 clipboard = QApplication::clipboard();
78//! [3]
79
80//! [4]
81 connect(fontCombo, SIGNAL(currentFontChanged(QFont)),
82 this, SLOT(findStyles(QFont)));
83 connect(fontCombo, SIGNAL(currentFontChanged(QFont)),
84 this, SLOT(findSizes(QFont)));
85 connect(fontCombo, SIGNAL(currentFontChanged(QFont)),
86 characterWidget, SLOT(updateFont(QFont)));
87 connect(sizeCombo, SIGNAL(currentIndexChanged(QString)),
88 characterWidget, SLOT(updateSize(QString)));
89 connect(styleCombo, SIGNAL(currentIndexChanged(QString)),
90 characterWidget, SLOT(updateStyle(QString)));
91//! [4] //! [5]
92 connect(characterWidget, SIGNAL(characterSelected(QString)),
93 this, SLOT(insertCharacter(QString)));
94 connect(clipboardButton, SIGNAL(clicked()), this, SLOT(updateClipboard()));
95//! [5]
96 connect(fontMerging, SIGNAL(toggled(bool)), characterWidget, SLOT(updateFontMerging(bool)));
97
98//! [6]
99 QHBoxLayout *controlsLayout = new QHBoxLayout;
100 controlsLayout->addWidget(fontLabel);
101 controlsLayout->addWidget(fontCombo, 1);
102 controlsLayout->addWidget(sizeLabel);
103 controlsLayout->addWidget(sizeCombo, 1);
104 controlsLayout->addWidget(styleLabel);
105 controlsLayout->addWidget(styleCombo, 1);
106 controlsLayout->addWidget(fontMergingLabel);
107 controlsLayout->addWidget(fontMerging, 1);
108 controlsLayout->addStretch(1);
109
110 QHBoxLayout *lineLayout = new QHBoxLayout;
111 lineLayout->addWidget(lineEdit, 1);
112 lineLayout->addSpacing(12);
113 lineLayout->addWidget(clipboardButton);
114
115 QVBoxLayout *centralLayout = new QVBoxLayout;
116 centralLayout->addLayout(controlsLayout);
117 centralLayout->addWidget(scrollArea, 1);
118 centralLayout->addSpacing(4);
119 centralLayout->addLayout(lineLayout);
120 centralWidget->setLayout(centralLayout);
121
122 setCentralWidget(centralWidget);
123 setWindowTitle(tr("Character Map"));
124}
125//! [6]
126
127//! [7]
128void MainWindow::findStyles(const QFont &font)
129{
130 QFontDatabase fontDatabase;
131 QString currentItem = styleCombo->currentText();
132 styleCombo->clear();
133//! [7]
134
135//! [8]
136 QString style;
137 foreach (style, fontDatabase.styles(font.family()))
138 styleCombo->addItem(style);
139
140 int styleIndex = styleCombo->findText(currentItem);
141
142 if (styleIndex == -1)
143 styleCombo->setCurrentIndex(0);
144 else
145 styleCombo->setCurrentIndex(styleIndex);
146}
147//! [8]
148
149void MainWindow::findSizes(const QFont &font)
150{
151 QFontDatabase fontDatabase;
152 QString currentSize = sizeCombo->currentText();
153 sizeCombo->blockSignals(true);
154 sizeCombo->clear();
155
156 int size;
157 if(fontDatabase.isSmoothlyScalable(font.family(), fontDatabase.styleString(font))) {
158 foreach(size, QFontDatabase::standardSizes()) {
159 sizeCombo->addItem(QVariant(size).toString());
160 sizeCombo->setEditable(true);
161 }
162
163 } else {
164 foreach(size, fontDatabase.smoothSizes(font.family(), fontDatabase.styleString(font))) {
165 sizeCombo->addItem(QVariant(size).toString());
166 sizeCombo->setEditable(false);
167 }
168 }
169
170 sizeCombo->blockSignals(false);
171
172 int sizeIndex = sizeCombo->findText(currentSize);
173
174 if(sizeIndex == -1)
175 sizeCombo->setCurrentIndex(qMax(0, sizeCombo->count() / 3));
176 else
177 sizeCombo->setCurrentIndex(sizeIndex);
178}
179
180//! [9]
181void MainWindow::insertCharacter(const QString &character)
182{
183 lineEdit->insert(character);
184}
185//! [9]
186
187//! [10]
188void MainWindow::updateClipboard()
189{
190//! [11]
191 clipboard->setText(lineEdit->text(), QClipboard::Clipboard);
192//! [11]
193 clipboard->setText(lineEdit->text(), QClipboard::Selection);
194}
195//! [10]
Note: See TracBrowser for help on using the repository browser.