| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include <QtGui> | 
|---|
| 43 |  | 
|---|
| 44 | #include "characterwidget.h" | 
|---|
| 45 | #include "mainwindow.h" | 
|---|
| 46 |  | 
|---|
| 47 | //! [0] | 
|---|
| 48 | MainWindow::MainWindow() | 
|---|
| 49 | { | 
|---|
| 50 | QWidget *centralWidget = new QWidget; | 
|---|
| 51 |  | 
|---|
| 52 | QLabel *fontLabel = new QLabel(tr("Font:")); | 
|---|
| 53 | fontCombo = new QFontComboBox; | 
|---|
| 54 | QLabel *sizeLabel = new QLabel(tr("Size:")); | 
|---|
| 55 | sizeCombo = new QComboBox; | 
|---|
| 56 | QLabel *styleLabel = new QLabel(tr("Style:")); | 
|---|
| 57 | styleCombo = new QComboBox; | 
|---|
| 58 | QLabel *fontMergingLabel = new QLabel(tr("Automatic Font Merging:")); | 
|---|
| 59 | fontMerging = new QCheckBox; | 
|---|
| 60 | fontMerging->setChecked(true); | 
|---|
| 61 |  | 
|---|
| 62 | scrollArea = new QScrollArea; | 
|---|
| 63 | characterWidget = new CharacterWidget; | 
|---|
| 64 | scrollArea->setWidget(characterWidget); | 
|---|
| 65 | //! [0] | 
|---|
| 66 |  | 
|---|
| 67 | //! [1] | 
|---|
| 68 | findStyles(fontCombo->currentFont()); | 
|---|
| 69 | //! [1] | 
|---|
| 70 | findSizes(fontCombo->currentFont()); | 
|---|
| 71 |  | 
|---|
| 72 | //! [2] | 
|---|
| 73 | lineEdit = new QLineEdit; | 
|---|
| 74 | QPushButton *clipboardButton = new QPushButton(tr("&To clipboard")); | 
|---|
| 75 | //! [2] | 
|---|
| 76 |  | 
|---|
| 77 | //! [3] | 
|---|
| 78 | clipboard = QApplication::clipboard(); | 
|---|
| 79 | //! [3] | 
|---|
| 80 |  | 
|---|
| 81 | //! [4] | 
|---|
| 82 | connect(fontCombo, SIGNAL(currentFontChanged(const QFont &)), | 
|---|
| 83 | this, SLOT(findStyles(const QFont &))); | 
|---|
| 84 | connect(fontCombo, SIGNAL(currentFontChanged(const QFont &)), | 
|---|
| 85 | this, SLOT(findSizes(const QFont &))); | 
|---|
| 86 | connect(fontCombo, SIGNAL(currentFontChanged(const QFont &)), | 
|---|
| 87 | characterWidget, SLOT(updateFont(const QFont &))); | 
|---|
| 88 | connect(sizeCombo, SIGNAL(currentIndexChanged(const QString &)), | 
|---|
| 89 | characterWidget, SLOT(updateSize(const QString &))); | 
|---|
| 90 | connect(styleCombo, SIGNAL(currentIndexChanged(const QString &)), | 
|---|
| 91 | characterWidget, SLOT(updateStyle(const QString &))); | 
|---|
| 92 | //! [4] //! [5] | 
|---|
| 93 | connect(characterWidget, SIGNAL(characterSelected(const QString &)), | 
|---|
| 94 | this, SLOT(insertCharacter(const QString &))); | 
|---|
| 95 | connect(clipboardButton, SIGNAL(clicked()), this, SLOT(updateClipboard())); | 
|---|
| 96 | //! [5] | 
|---|
| 97 | connect(fontMerging, SIGNAL(toggled(bool)), characterWidget, SLOT(updateFontMerging(bool))); | 
|---|
| 98 |  | 
|---|
| 99 | //! [6] | 
|---|
| 100 | QHBoxLayout *controlsLayout = new QHBoxLayout; | 
|---|
| 101 | controlsLayout->addWidget(fontLabel); | 
|---|
| 102 | controlsLayout->addWidget(fontCombo, 1); | 
|---|
| 103 | controlsLayout->addWidget(sizeLabel); | 
|---|
| 104 | controlsLayout->addWidget(sizeCombo, 1); | 
|---|
| 105 | controlsLayout->addWidget(styleLabel); | 
|---|
| 106 | controlsLayout->addWidget(styleCombo, 1); | 
|---|
| 107 | controlsLayout->addWidget(fontMergingLabel); | 
|---|
| 108 | controlsLayout->addWidget(fontMerging, 1); | 
|---|
| 109 | controlsLayout->addStretch(1); | 
|---|
| 110 |  | 
|---|
| 111 | QHBoxLayout *lineLayout = new QHBoxLayout; | 
|---|
| 112 | lineLayout->addWidget(lineEdit, 1); | 
|---|
| 113 | lineLayout->addSpacing(12); | 
|---|
| 114 | lineLayout->addWidget(clipboardButton); | 
|---|
| 115 |  | 
|---|
| 116 | QVBoxLayout *centralLayout = new QVBoxLayout; | 
|---|
| 117 | centralLayout->addLayout(controlsLayout); | 
|---|
| 118 | centralLayout->addWidget(scrollArea, 1); | 
|---|
| 119 | centralLayout->addSpacing(4); | 
|---|
| 120 | centralLayout->addLayout(lineLayout); | 
|---|
| 121 | centralWidget->setLayout(centralLayout); | 
|---|
| 122 |  | 
|---|
| 123 | setCentralWidget(centralWidget); | 
|---|
| 124 | setWindowTitle(tr("Character Map")); | 
|---|
| 125 | } | 
|---|
| 126 | //! [6] | 
|---|
| 127 |  | 
|---|
| 128 | //! [7] | 
|---|
| 129 | void MainWindow::findStyles(const QFont &font) | 
|---|
| 130 | { | 
|---|
| 131 | QFontDatabase fontDatabase; | 
|---|
| 132 | QString currentItem = styleCombo->currentText(); | 
|---|
| 133 | styleCombo->clear(); | 
|---|
| 134 | //! [7] | 
|---|
| 135 |  | 
|---|
| 136 | //! [8] | 
|---|
| 137 | QString style; | 
|---|
| 138 | foreach (style, fontDatabase.styles(font.family())) | 
|---|
| 139 | styleCombo->addItem(style); | 
|---|
| 140 |  | 
|---|
| 141 | int styleIndex = styleCombo->findText(currentItem); | 
|---|
| 142 |  | 
|---|
| 143 | if (styleIndex == -1) | 
|---|
| 144 | styleCombo->setCurrentIndex(0); | 
|---|
| 145 | else | 
|---|
| 146 | styleCombo->setCurrentIndex(styleIndex); | 
|---|
| 147 | } | 
|---|
| 148 | //! [8] | 
|---|
| 149 |  | 
|---|
| 150 | void MainWindow::findSizes(const QFont &font) | 
|---|
| 151 | { | 
|---|
| 152 | QFontDatabase fontDatabase; | 
|---|
| 153 | QString currentSize = sizeCombo->currentText(); | 
|---|
| 154 | sizeCombo->blockSignals(true); | 
|---|
| 155 | sizeCombo->clear(); | 
|---|
| 156 |  | 
|---|
| 157 | int size; | 
|---|
| 158 | if(fontDatabase.isSmoothlyScalable(font.family(), fontDatabase.styleString(font))) { | 
|---|
| 159 | foreach(size, QFontDatabase::standardSizes()) { | 
|---|
| 160 | sizeCombo->addItem(QVariant(size).toString()); | 
|---|
| 161 | sizeCombo->setEditable(true); | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | } else { | 
|---|
| 165 | foreach(size, fontDatabase.smoothSizes(font.family(), fontDatabase.styleString(font))) { | 
|---|
| 166 | sizeCombo->addItem(QVariant(size).toString()); | 
|---|
| 167 | sizeCombo->setEditable(false); | 
|---|
| 168 | } | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | sizeCombo->blockSignals(false); | 
|---|
| 172 |  | 
|---|
| 173 | int sizeIndex = sizeCombo->findText(currentSize); | 
|---|
| 174 |  | 
|---|
| 175 | if(sizeIndex == -1) | 
|---|
| 176 | sizeCombo->setCurrentIndex(qMax(0, sizeCombo->count() / 3)); | 
|---|
| 177 | else | 
|---|
| 178 | sizeCombo->setCurrentIndex(sizeIndex); | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | //! [9] | 
|---|
| 182 | void MainWindow::insertCharacter(const QString &character) | 
|---|
| 183 | { | 
|---|
| 184 | lineEdit->insert(character); | 
|---|
| 185 | } | 
|---|
| 186 | //! [9] | 
|---|
| 187 |  | 
|---|
| 188 | //! [10] | 
|---|
| 189 | void MainWindow::updateClipboard() | 
|---|
| 190 | { | 
|---|
| 191 | //! [11] | 
|---|
| 192 | clipboard->setText(lineEdit->text(), QClipboard::Clipboard); | 
|---|
| 193 | //! [11] | 
|---|
| 194 | clipboard->setText(lineEdit->text(), QClipboard::Selection); | 
|---|
| 195 | } | 
|---|
| 196 | //! [10] | 
|---|