examples/widgets/charactermap/mainwindow.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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]