diff options
Diffstat (limited to 'examples/widgets')
201 files changed, 15819 insertions, 1 deletions
diff --git a/examples/widgets/doc/images/completer-example-country.png b/examples/widgets/doc/images/completer-example-country.png Binary files differnew file mode 100644 index 0000000000..fa7c8a9938 --- /dev/null +++ b/examples/widgets/doc/images/completer-example-country.png diff --git a/examples/widgets/doc/images/completer-example-word.png b/examples/widgets/doc/images/completer-example-word.png Binary files differnew file mode 100644 index 0000000000..aa3fb9c333 --- /dev/null +++ b/examples/widgets/doc/images/completer-example-word.png diff --git a/examples/widgets/doc/images/completer-example.png b/examples/widgets/doc/images/completer-example.png Binary files differnew file mode 100644 index 0000000000..dcaa253bd8 --- /dev/null +++ b/examples/widgets/doc/images/completer-example.png diff --git a/examples/widgets/doc/images/echopluginexample.png b/examples/widgets/doc/images/echopluginexample.png Binary files differnew file mode 100644 index 0000000000..7cb1e4d63b --- /dev/null +++ b/examples/widgets/doc/images/echopluginexample.png diff --git a/examples/widgets/doc/images/plugandpaint-plugindialog.png b/examples/widgets/doc/images/plugandpaint-plugindialog.png Binary files differnew file mode 100644 index 0000000000..4b601bd58b --- /dev/null +++ b/examples/widgets/doc/images/plugandpaint-plugindialog.png diff --git a/examples/widgets/doc/images/plugandpaint.png b/examples/widgets/doc/images/plugandpaint.png Binary files differnew file mode 100644 index 0000000000..bd5d001f91 --- /dev/null +++ b/examples/widgets/doc/images/plugandpaint.png diff --git a/examples/widgets/doc/images/regexp-example.png b/examples/widgets/doc/images/regexp-example.png Binary files differnew file mode 100644 index 0000000000..0f31a2f93f --- /dev/null +++ b/examples/widgets/doc/images/regexp-example.png diff --git a/examples/widgets/doc/images/settingseditor-example.png b/examples/widgets/doc/images/settingseditor-example.png Binary files differnew file mode 100644 index 0000000000..7a5be05fd0 --- /dev/null +++ b/examples/widgets/doc/images/settingseditor-example.png diff --git a/examples/widgets/doc/images/stylepluginexample.png b/examples/widgets/doc/images/stylepluginexample.png Binary files differnew file mode 100644 index 0000000000..05d8c6b5cf --- /dev/null +++ b/examples/widgets/doc/images/stylepluginexample.png diff --git a/examples/widgets/doc/images/textfinder-example-userinterface.png b/examples/widgets/doc/images/textfinder-example-userinterface.png Binary files differnew file mode 100644 index 0000000000..2bebe2e9dd --- /dev/null +++ b/examples/widgets/doc/images/textfinder-example-userinterface.png diff --git a/examples/widgets/doc/images/treemodelcompleter-example.png b/examples/widgets/doc/images/treemodelcompleter-example.png Binary files differnew file mode 100644 index 0000000000..000405fe39 --- /dev/null +++ b/examples/widgets/doc/images/treemodelcompleter-example.png diff --git a/examples/widgets/doc/images/undodemo.png b/examples/widgets/doc/images/undodemo.png Binary files differnew file mode 100644 index 0000000000..85c3622738 --- /dev/null +++ b/examples/widgets/doc/images/undodemo.png diff --git a/examples/widgets/doc/images/undoframeworkexample.png b/examples/widgets/doc/images/undoframeworkexample.png Binary files differnew file mode 100644 index 0000000000..7e0a1df260 --- /dev/null +++ b/examples/widgets/doc/images/undoframeworkexample.png diff --git a/examples/widgets/doc/src/completer.qdoc b/examples/widgets/doc/src/completer.qdoc new file mode 100644 index 0000000000..39be735245 --- /dev/null +++ b/examples/widgets/doc/src/completer.qdoc @@ -0,0 +1,249 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example tools/completer + \title Completer Example + + The Completer example shows how to provide string-completion facilities + for an input widget based on data provided by a model. + + \image completer-example.png + + This example uses a custom item model, \c FileSystemModel, and a QCompleter object. + QCompleter is a class that provides completions based on an item model. The + type of model, the completion mode, and the case sensitivity can be + selected using combo boxes. + + \section1 The Resource File + + The Completer example requires a resource file in order to store the + \e{countries.txt} and \e{words.txt}. The resource file contains the + following code: + + \quotefile tools/completer/completer.qrc + + \section1 FileSystemModel Class Definition + + The \c FileSystemModel class is a subclass of QFileSystemModel, which provides a data + model for the local filesystem. + + \snippet tools/completer/fsmodel.h 0 + + This class only has a constructor and a \c data() function as it is only + created to enable \c data() to return the entire file path for the + display role, unlike \l{QFileSystemModel}'s \c data() function that only returns + the folder and not the drive label. This is further explained in + \c FileSystemModel's implementation. + + \section1 FileSystemModel Class Implementation + + The constructor for the \c FileSystemModel class is used to pass \a parent to + QFileSystemModel. + + \snippet tools/completer/fsmodel.cpp 0 + + As mentioned earlier, the \c data() function is reimplemented in order to + get it to return the entire file parth for the display role. For example, + with a QFileSystemModel, you will see "Program Files" in the view. However, with + \c FileSystemModel, you will see "C:\\Program Files". + + \snippet tools/completer/fsmodel.cpp 1 + + The screenshots below illustrate this difference: + + \table + \row \li \inlineimage completer-example-qdirmodel.png + \li \inlineimage completer-example-dirmodel.png + \endtable + + The Qt::EditRole, which QCompleter uses to look for matches, is left + unchanged. + + \section1 MainWindow Class Definition + + The \c MainWindow class is a subclass of QMainWindow and implements five + private slots - \c about(), \c changeCase(), \c changeMode(), \c changeModel(), + and \c changeMaxVisible(). + + \snippet tools/completer/mainwindow.h 0 + + Within the \c MainWindow class, we have two private functions: + \c createMenu() and \c modelFromFile(). We also declare the private widgets + needed - three QComboBox objects, a QCheckBox, a QCompleter, a QLabel, and + a QLineEdit. + + \snippet tools/completer/mainwindow.h 1 + + \section1 MainWindow Class Implementation + + The constructor of \c MainWindow constructs a \c MainWindow with a parent + widget and initializes the private members. The \c createMenu() function + is then invoked. + + We set up three QComboBox objects, \c modelComb, \c modeCombo and + \c caseCombo. By default, the \c modelCombo is set to QFileSystemModel, + the \c modeCombo is set to "Filtered Popup" and the \c caseCombo is set + to "Case Insensitive". + + \snippet tools/completer/mainwindow.cpp 0 + + The \c maxVisibleSpinBox is created and determines the number of visible + item in the completer + + The \c wrapCheckBox is then set up. This \c checkBox determines if the + \c{completer}'s \l{QCompleter::setWrapAround()}{setWrapAround()} property + is enabled or disabled. + + \snippet tools/completer/mainwindow.cpp 1 + + We instantiate \c contentsLabel and set its size policy to + \l{QSizePolicy::Fixed}{fixed}. The combo boxes' \l{QComboBox::activated()} + {activated()} signals are then connected to their respective slots. + + \snippet tools/completer/mainwindow.cpp 2 + + The \c lineEdit is set up and then we arrange all the widgets using a + QGridLayout. The \c changeModel() function is called, to initialize the + \c completer. + + \snippet tools/completer/mainwindow.cpp 3 + + The \c createMenu() function is used to instantiate the QAction objects + needed to fill the \c fileMenu and \c helpMenu. The actions' + \l{QAction::triggered()}{triggered()} signals are connected to their + respective slots. + + \snippet tools/completer/mainwindow.cpp 4 + + The \c modelFromFile() function accepts the \a fileName of a file and + processes it depending on its contents. + + We first validate the \c file to ensure that it can be opened in + QFile::ReadOnly mode. If this is unsuccessful, the function returns an + empty QStringListModel. + + \snippet tools/completer/mainwindow.cpp 5 + + The mouse cursor is then overridden with Qt::WaitCursor before we fill + a QStringList object, \c words, with the contents of \c file. Once this + is done, we restore the mouse cursor. + + \snippet tools/completer/mainwindow.cpp 6 + + As mentioned earlier, the resources file contains two files - + \e{countries.txt} and \e{words.txt}. If the \c file read is \e{words.txt}, + we return a QStringListModel with \c words as its QStringList and + \c completer as its parent. + + \snippet tools/completer/mainwindow.cpp 7 + + If the \c file read is \e{countries.txt}, then we require a + QStandardItemModel with \c words.count() rows, 2 columns, and \c completer + as its parent. + + \snippet tools/completer/mainwindow.cpp 8 + + A standard line in \e{countries.txt} is: + \quotation + Norway NO + \endquotation + + Hence, to populate the QStandardItemModel object, \c m, we have to + split the country name and its symbol. Once this is done, we return + \c m. + + \snippet tools/completer/mainwindow.cpp 9 + + The \c changeMode() function sets the \c{completer}'s mode, depending on + the value of \c index. + + \snippet tools/completer/mainwindow.cpp 10 + + The \c changeModel() function changes the item model used based on the + model selected by the user. + + A \c switch statement is used to change the item model based on the index + of \c modelCombo. If \c case is 0, we use an unsorted QFileSystemModel, providing + us with a file path excluding the drive label. + + \snippet tools/completer/mainwindow.cpp 11 + + Note that we create the model with \c completer as the parent as this + allows us to replace the model with a new model. The \c completer will + ensure that the old one is deleted the moment a new model is assigned + to it. + + If \c case is 1, we use the \c DirModel we defined earlier, resulting in + full paths for the files. + + \snippet tools/completer/mainwindow.cpp 12 + + When \c case is 2, we attempt to complete names of countries. This requires + a QTreeView object, \c treeView. The country names are extracted from + \e{countries.txt} and set the popup used to display completions to + \c treeView. + + \snippet tools/completer/mainwindow.cpp 13 + + The screenshot below shows the Completer with the country list model. + + \image completer-example-country.png + + If \c case is 3, we attempt to complete words. This is done using a + QStringListModel that contains data extracted from \e{words.txt}. The + model is sorted \l{QCompleter::CaseInsensitivelySortedModel} + {case insensitively}. + + The screenshot below shows the Completer with the word list model. + + \image completer-example-word.png + + Once the model type is selected, we call the \c changeMode() function and + the \c changeCase() function and set the wrap option accordingly. The + \c{wrapCheckBox}'s \l{QCheckBox::clicked()}{clicked()} signal is connected + to the \c{completer}'s \l{QCompleter::setWrapAround()}{setWrapAround()} + slot. + + \snippet tools/completer/mainwindow.cpp 14 + + The \c changeMaxVisible() update the maximum number of visible items in + the completer. + + \snippet tools/completer/mainwindow.cpp 15 + + The \c about() function provides a brief description about the example. + + \snippet tools/completer/mainwindow.cpp 16 + + \section1 \c main() Function + + The \c main() function instantiates QApplication and \c MainWindow and + invokes the \l{QWidget::show()}{show()} function. + + \snippet tools/completer/main.cpp 0 + */ diff --git a/examples/widgets/doc/src/echoplugin.qdoc b/examples/widgets/doc/src/echoplugin.qdoc new file mode 100644 index 0000000000..81ba18cdff --- /dev/null +++ b/examples/widgets/doc/src/echoplugin.qdoc @@ -0,0 +1,208 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example tools/echoplugin + \title Echo Plugin Example + + This example shows how to create a Qt plugin. + + \image echopluginexample.png + + There are two kinds of plugins in Qt: plugins that extend Qt + itself and plugins that extend applications written in Qt. In this + example, we show the procedure of implementing plugins that extend + applications. When you create a plugin you declare an interface, + which is a class with only pure virtual functions. This interface + is inherited by the class that implements the plugin. The class is + stored in a shared library and can therefore be loaded by + applications at run-time. When loaded, the plugin is dynamically + cast to the interface using Qt's \l{Meta-Object + System}{meta-object system}. The plugin \l{How to Create Qt + Plugins}{overview document} gives a high-level introduction to + plugins. + + We have implemented a plugin, the \c EchoPlugin, which implements + the \c EchoInterface. The interface consists of \c echo(), which + takes a QString as argument. The \c EchoPlugin returns the string + unaltered (i.e., it works as the familiar echo command found in + both Unix and Windows). + + We test the plugin in \c EchoWindow: when you push the QPushButton + (as seen in the image above), the application sends the text in + the QLineEdit to the plugin, which echoes it back to the + application. The answer from the plugin is displayed in the + QLabel. + + + \section1 EchoWindow Class Definition + + The \c EchoWindow class lets us test the \c EchoPlugin through a + GUI. + + \snippet tools/echoplugin/echowindow/echowindow.h 0 + + We load the plugin in \c loadPlugin() and cast it to \c + EchoInterface. When the user clicks the \c button we take the + text in \c lineEdit and call the interface's \c echo() with it. + + + \section1 EchoWindow Class Implementation + + We start with a look at the constructor: + + \snippet tools/echoplugin/echowindow/echowindow.cpp 0 + + We create the widgets and set a title for the window. We then load + the plugin. \c loadPlugin() returns false if the plugin could not + be loaded, in which case we disable the widgets. If you wish a + more detailed error message, you can use + \l{QPluginLoader::}{errorString()}; we will look more closely at + QPluginLoader later. + + Here is the implementation of \c sendEcho(): + + \snippet tools/echoplugin/echowindow/echowindow.cpp 1 + + This slot is called when the user pushes \c button or presses + enter in \c lineEdit. We call \c echo() of the echo interface. In + our example this is the \c EchoPlugin, but it could be any plugin + that inherit the \c EchoInterface. We take the QString returned + from \c echo() and display it in the \c label. + + Here is the implementation of \c createGUI(): + + \snippet tools/echoplugin/echowindow/echowindow.cpp 2 + + We create the widgets and lay them out in a grid layout. We + connect the label and line edit to our \c sendEcho() slot. + + Here is the \c loadPlugin() function: + + \snippet tools/echoplugin/echowindow/echowindow.cpp 3 + + Access to plugins at run-time is provided by QPluginLoader. You + supply it with the filename of the shared library the plugin is + stored in and call \l{QPluginLoader::}{instance()}, which loads + and returns the root component of the plugin (i.e., it resolves + the type of the plugin and creates a QObject instance of it). If + the plugin was not successfully loaded, it will be null, so we + return false. If it was loaded correctly, we can cast the plugin + to our \c EchoInterface and return true. In the case that the + plugin loaded does not implement the \c EchoInterface, \c + instance() will return null, but this cannot happen in our + example. Notice that the location of the plugin is not the same + for all platforms. + + + \section1 EchoInterface Class Definition + + The \c EchoInterface defines the functions that the plugin will + provide. An interface is a class that only consists of pure + virtual functions. If non virtual functions were present in the + class you would get misleading compile errors in the moc files. + + \snippet tools/echoplugin/echowindow/echointerface.h 0 + + We declare \c echo(). In our \c EchoPlugin we use this method to + return, or echo, \a message. + + We use the Q_DECLARE_INTERFACE macro to let \l{Meta-Object + System}{Qt's meta object system} aware of the interface. We do + this so that it will be possible to identify plugins that + implements the interface at run-time. The second argument is a + string that must identify the interface in a unique way. + + + \section1 EchoPlugin Class Definition + + We inherit both QObject and \c EchoInterface to make this class a + plugin. The Q_INTERFACES macro tells Qt which interfaces the class + implements. In our case we only implement the \c EchoInterface. + If a class implements more than one interface, they are given as + a comma separated list. + + \snippet tools/echoplugin/plugin/echoplugin.h 0 + + + \section1 EchoPlugin Class Implementation + + Here is the implementation of \c echo(): + + \snippet tools/echoplugin/plugin/echoplugin.cpp 0 + + We simply return the functions parameter. + + \snippet tools/echoplugin/plugin/echoplugin.cpp 1 + + We use the Q_EXPORT_PLUGIN2 macro to let Qt know that the \c + EchoPlugin class is a plugin. The first parameter is the name of + the plugin; it is usual to give the plugin and the library file it + is stored in the same name. + + \section1 The \c main() function + + \snippet tools/echoplugin/echowindow/main.cpp 0 + + We create an \c EchoWindow and display it as a top-level window. + + \section1 The Profiles + + When creating plugins the profiles need to be adjusted. + We show here what changes need to be done. + + The profile in the echoplugin directory uses the \c subdirs + template and simply includes includes to directories in which + the echo window and echo plugin lives: + + \snippet tools/echoplugin/echoplugin.pro 0 + + The profile for the echo window does not need any plugin specific + settings. We move on to the plugin profile: + + \snippet tools/echoplugin/plugin/plugin.pro 0 + + We need to set the TEMPLATE as we now want to make a library + instead of an executable. We also need to tell qmake that we are + creating a plugin. The \c EchoInterface that the plugin implements + lives in the \c echowindow directory, so we need to add that + directory to the include path. We set the TARGET of the project, + which is the name of the library file in which the plugin will be + stored; qmake appends the appropriate file extension depending on + the platform. By convention the target should have the same name + as the plugin (set with Q_EXPORT_PLUGIN2) + + \section1 Further reading and examples + + You can find an overview of the macros needed to create plugins + \l{Macros for Defining Plugins}{here}. + + We give an example of a plugin that extend Qt in the \l{Style + Plugin Example}{style plugin} example. The \l{Plug & Paint + Example}{plug and paint} example shows how to create static + plugins. +*/ diff --git a/examples/widgets/doc/src/i18n.qdoc b/examples/widgets/doc/src/i18n.qdoc new file mode 100644 index 0000000000..980319233d --- /dev/null +++ b/examples/widgets/doc/src/i18n.qdoc @@ -0,0 +1,37 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example tools/i18n + \title I18N Example + + The Internationalization (I18N) example demonstrates Qt's support for translated + text. Developers can write the initial application text in one language, and + translations can be provided later without any modifications to the code. + + \image i18n-example.png +*/ diff --git a/examples/widgets/doc/src/plugandpaint.qdoc b/examples/widgets/doc/src/plugandpaint.qdoc new file mode 100644 index 0000000000..24fb4ab1b9 --- /dev/null +++ b/examples/widgets/doc/src/plugandpaint.qdoc @@ -0,0 +1,540 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example tools/plugandpaint + \title Plug & Paint Example + + The Plug & Paint example demonstrates how to write Qt + applications that can be extended through plugins. + + \image plugandpaint.png Screenshot of the Plug & Paint example + + A plugin is a dynamic library that can be loaded at run-time to + extend an application. Qt makes it possible to create custom + plugins and to load them using QPluginLoader. To ensure that + plugins don't get lost, it is also possible to link them + statically to the executable. The Plug & Paint example uses + plugins to support custom brushes, shapes, and image filters. A + single plugin can provide multiple brushes, shapes, and/or + filters. + + If you want to learn how to make your own application extensible + through plugins, we recommend that you start by reading this + overview, which explains how to make an application use plugins. + Afterward, you can read the + \l{plugandpaintplugins/basictools}{Basic Tools} and + \l{plugandpaintplugins/extrafilters}{Extra Filters} + overviews, which show how to implement static and dynamic + plugins, respectively. + + Plug & Paint consists of the following classes: + + \list + \li \c MainWindow is a QMainWindow subclass that provides the menu + system and that contains a \c PaintArea as the central widget. + \li \c PaintArea is a QWidget that allows the user to draw using a + brush and to insert shapes. + \li \c PluginDialog is a dialog that shows information about the + plugins detected by the application. + \li \c BrushInterface, \c ShapeInterface, and \c FilterInterface are + abstract base classes that can be implemented by plugins to + provide custom brushes, shapes, and image filters. + \endlist + + \section1 The Plugin Interfaces + + We will start by reviewing the interfaces defined in \c + interfaces.h. These interfaces are used by the Plug & Paint + application to access extra functionality. They are implemented + in the plugins. + + + \snippet tools/plugandpaint/interfaces.h 0 + + The \c BrushInterface class declares four pure virtual functions. + The first pure virtual function, \c brushes(), returns a list of + strings that identify the brushes provided by the plugin. By + returning a QStringList instead of a QString, we make it possible + for a single plugin to provide multiple brushes. The other + functions have a \c brush parameter to identify which brush + (among those returned by \c brushes()) is used. + + \c mousePress(), \c mouseMove(), and \c mouseRelease() take a + QPainter and one or two \l{QPoint}s, and return a QRect + identifying which portion of the image was altered by the brush. + + The class also has a virtual destructor. Interface classes + usually don't need such a destructor (because it would make + little sense to \c delete the object that implements the + interface through a pointer to the interface), but some compilers + emit a warning for classes that declare virtual functions but no + virtual destructor. We provide the destructor to keep these + compilers happy. + + \snippet tools/plugandpaint/interfaces.h 1 + + The \c ShapeInterface class declares a \c shapes() function that + works the same as \c{BrushInterface}'s \c brushes() function, and + a \c generateShape() function that has a \c shape parameter. + Shapes are represented by a QPainterPath, a data type that can + represent arbitrary 2D shapes or combinations of shapes. The \c + parent parameter can be used by the plugin to pop up a dialog + asking the user to specify more information. + + \snippet tools/plugandpaint/interfaces.h 2 + + The \c FilterInterface class declares a \c filters() function + that returns a list of filter names, and a \c filterImage() + function that applies a filter to an image. + + \snippet tools/plugandpaint/interfaces.h 4 + + To make it possible to query at run-time whether a plugin + implements a given interface, we must use the \c + Q_DECLARE_INTERFACE() macro. The first argument is the name of + the interface. The second argument is a string identifying the + interface in a unique way. By convention, we use a "Java package + name" syntax to identify interfaces. If we later change the + interfaces, we must use a different string to identify the new + interface; otherwise, the application might crash. It is therefore + a good idea to include a version number in the string, as we did + above. + + The \l{plugandpaintplugins/basictools}{Basic Tools} plugin + and the \l{plugandpaintplugins/extrafilters}{Extra Filters} + plugin shows how to derive from \c BrushInterface, \c + ShapeInterface, and \c FilterInterface. + + A note on naming: It might have been tempting to give the \c + brushes(), \c shapes(), and \c filters() functions a more generic + name, such as \c keys() or \c features(). However, that would + have made multiple inheritance impractical. When creating + interfaces, we should always try to give unique names to the pure + virtual functions. + + \section1 The MainWindow Class + + The \c MainWindow class is a standard QMainWindow subclass, as + found in many of the other examples (e.g., + \l{mainwindows/application}{Application}). Here, we'll + concentrate on the parts of the code that are related to plugins. + + \snippet tools/plugandpaint/mainwindow.cpp 4 + + The \c loadPlugins() function is called from the \c MainWindow + constructor to detect plugins and update the \uicontrol{Brush}, + \uicontrol{Shapes}, and \uicontrol{Filters} menus. We start by handling static + plugins (available through QPluginLoader::staticInstances()) + + To the application that uses the plugin, a Qt plugin is simply a + QObject. That QObject implements plugin interfaces using multiple + inheritance. + + \snippet tools/plugandpaint/mainwindow.cpp 5 + + The next step is to load dynamic plugins. We initialize the \c + pluginsDir member variable to refer to the \c plugins + subdirectory of the Plug & Paint example. On Unix, this is just a + matter of initializing the QDir variable with + QApplication::applicationDirPath(), the path of the executable + file, and to do a \l{QDir::cd()}{cd()}. On Windows and Mac OS X, + this file is usually located in a subdirectory, so we need to + take this into account. + + \snippet tools/plugandpaint/mainwindow.cpp 6 + \snippet tools/plugandpaint/mainwindow.cpp 7 + \snippet tools/plugandpaint/mainwindow.cpp 8 + + We use QDir::entryList() to get a list of all files in that + directory. Then we iterate over the result using \l foreach and + try to load the plugin using QPluginLoader. + + The QObject provided by the plugin is accessible through + QPluginLoader::instance(). If the dynamic library isn't a Qt + plugin, or if it was compiled against an incompatible version of + the Qt library, QPluginLoader::instance() returns a null pointer. + + If QPluginLoader::instance() is non-null, we add it to the menus. + + \snippet tools/plugandpaint/mainwindow.cpp 9 + + At the end, we enable or disable the \uicontrol{Brush}, \uicontrol{Shapes}, + and \uicontrol{Filters} menus based on whether they contain any items. + + \snippet tools/plugandpaint/mainwindow.cpp 10 + + For each plugin (static or dynamic), we check which interfaces it + implements using \l qobject_cast(). First, we try to cast the + plugin instance to a \c BrushInterface; if it works, we call the + private function \c addToMenu() with the list of brushes returned + by \c brushes(). Then we do the same with the \c ShapeInterface + and the \c FilterInterface. + + \snippet tools/plugandpaint/mainwindow.cpp 3 + + The \c aboutPlugins() slot is called on startup and can be + invoked at any time through the \uicontrol{About Plugins} action. It + pops up a \c PluginDialog, providing information about the loaded + plugins. + + \image plugandpaint-plugindialog.png Screenshot of the Plugin dialog + + + The \c addToMenu() function is called from \c loadPlugin() to + create \l{QAction}s for custom brushes, shapes, or filters and + add them to the relevant menu. The QAction is created with the + plugin from which it comes from as the parent; this makes it + convenient to get access to the plugin later. + + \snippet tools/plugandpaint/mainwindow.cpp 0 + + The \c changeBrush() slot is invoked when the user chooses one of + the brushes from the \uicontrol{Brush} menu. We start by finding out + which action invoked the slot using QObject::sender(). Then we + get the \c BrushInterface out of the plugin (which we + conveniently passed as the QAction's parent) and we call \c + PaintArea::setBrush() with the \c BrushInterface and the string + identifying the brush. Next time the user draws on the paint + area, \c PaintArea will use this brush. + + \snippet tools/plugandpaint/mainwindow.cpp 1 + + The \c insertShape() is invoked when the use chooses one of the + shapes from the \uicontrol{Shapes} menu. We retrieve the QAction that + invoked the slot, then the \c ShapeInterface associated with that + QAction, and finally we call \c ShapeInterface::generateShape() + to obtain a QPainterPath. + + \snippet tools/plugandpaint/mainwindow.cpp 2 + + The \c applyFilter() slot is similar: We retrieve the QAction + that invoked the slot, then the \c FilterInterface associated to + that QAction, and finally we call \c + FilterInterface::filterImage() to apply the filter onto the + current image. + + \section1 The PaintArea Class + + The \c PaintArea class contains some code that deals with \c + BrushInterface, so we'll review it briefly. + + \snippet tools/plugandpaint/paintarea.cpp 0 + + In \c setBrush(), we simply store the \c BrushInterface and the + brush that are given to us by \c MainWindow. + + \snippet tools/plugandpaint/paintarea.cpp 1 + + In the \l{QWidget::mouseMoveEvent()}{mouse move event handler}, + we call the \c BrushInterface::mouseMove() function on the + current \c BrushInterface, with the current brush. The mouse + press and mouse release handlers are very similar. + + \section1 The PluginDialog Class + + The \c PluginDialog class provides information about the loaded + plugins to the user. Its constructor takes a path to the plugins + and a list of plugin file names. It calls \c findPlugins() + to fill the QTreeWdiget with information about the plugins: + + \snippet tools/plugandpaint/plugindialog.cpp 0 + + The \c findPlugins() is very similar to \c + MainWindow::loadPlugins(). It uses QPluginLoader to access the + static and dynamic plugins. Its helper function \c + populateTreeWidget() uses \l qobject_cast() to find out which + interfaces are implemented by the plugins: + + \snippet tools/plugandpaint/plugindialog.cpp 1 + + \section1 Importing Static Plugins + + The \l{plugandpaintplugins/basictools}{Basic Tools} plugin + is built as a static plugin, to ensure that it is always + available to the application. This requires using the + Q_IMPORT_PLUGIN() macro somewhere in the application (in a \c + .cpp file) and specifying the plugin in the \c .pro file. + + For Plug & Paint, we have chosen to put Q_IMPORT_PLUGIN() in \c + main.cpp: + + \snippet tools/plugandpaint/main.cpp 0 + + The argument to Q_IMPORT_PLUGIN() is the plugin's name, as + specified with Q_EXPORT_PLUGIN2() in the \l{Exporting the + Plugin}{plugin}. + + In the \c .pro file, we need to specify the static library. + Here's the project file for building Plug & Paint: + + \snippet tools/plugandpaint/plugandpaint.pro 0 + + The \c LIBS line variable specifies the library \c pnp_basictools + located in the \c ../plugandpaintplugins/basictools directory. + (Although the \c LIBS syntax has a distinct Unix flavor, \c qmake + supports it on all platforms.) + + The \c CONFIG() code at the end is necessary for this example + because the example is part of the Qt distribution and Qt can be + configured to be built simultaneously in debug and in release + modes. You don't need to for your own plugin applications. + + This completes our review of the Plug & Paint application. At + this point, you might want to take a look at the + \l{plugandpaintplugins/basictools}{Basic Tools} example + plugin. +*/ + +/*! + \example tools/plugandpaintplugins/basictools + \title Plug & Paint Basic Tools Example + + The Basic Tools example is a static plugin for the + \l{plugandpaint}{Plug & Paint} example. It provides a set + of basic brushes, shapes, and filters. Through the Basic Tools + example, we will review the four steps involved in writing a Qt + plugin: + + \list 1 + \li Declare a plugin class. + \li Implement the interfaces provided by the plugin. + \li Export the plugin using the Q_EXPORT_PLUGIN2() macro. + \li Build the plugin using an adequate \c .pro file. + \endlist + + \section1 Declaration of the Plugin Class + + \snippet tools/plugandpaintplugins/basictools/basictoolsplugin.h 0 + + We start by including \c interfaces.h, which defines the plugin + interfaces for the \l{plugandpaint}{Plug & Paint} + application. For the \c #include to work, we need to add an \c + INCLUDEPATH entry to the \c .pro file with the path to Qt's \c + examples/tools directory. + + The \c BasicToolsPlugin class is a QObject subclass that + implements the \c BrushInterface, the \c ShapeInterface, and the + \c FilterInterface. This is done through multiple inheritance. + The \c Q_INTERFACES() macro is necessary to tell \l{moc}, Qt's + meta-object compiler, that the base classes are plugin + interfaces. Without the \c Q_INTERFACES() macro, we couldn't use + \l qobject_cast() in the \l{plugandpaint}{Plug & Paint} + application to detect interfaces. + + \snippet tools/plugandpaintplugins/basictools/basictoolsplugin.h 2 + + In the \c public section of the class, we declare all the + functions from the three interfaces. + + \section1 Implementation of the Brush Interface + + Let's now review the implementation of the \c BasicToolsPlugin + member functions inherited from \c BrushInterface. + + \snippet tools/plugandpaintplugins/basictools/basictoolsplugin.cpp 0 + + The \c brushes() function returns a list of brushes provided by + this plugin. We provide three brushes: \uicontrol{Pencil}, \uicontrol{Air + Brush}, and \uicontrol{Random Letters}. + + \snippet tools/plugandpaintplugins/basictools/basictoolsplugin.cpp 1 + + On a mouse press event, we just call \c mouseMove() to draw the + spot where the event occurred. + + \snippet tools/plugandpaintplugins/basictools/basictoolsplugin.cpp 2 + + In \c mouseMove(), we start by saving the state of the QPainter + and we compute a few variables that we'll need later. + + \snippet tools/plugandpaintplugins/basictools/basictoolsplugin.cpp 3 + + Then comes the brush-dependent part of the code: + + \list + \li If the brush is \uicontrol{Pencil}, we just call + QPainter::drawLine() with the current QPen. + + \li If the brush is \uicontrol{Air Brush}, we start by setting the + painter's QBrush to Qt::Dense6Pattern to obtain a dotted + pattern. Then we draw a circle filled with that QBrush several + times, resulting in a thick line. + + \li If the brush is \uicontrol{Random Letters}, we draw a random letter + at the new cursor position. Most of the code is for setting + the font to be bold and larger than the default font and for + computing an appropriate bounding rect. + \endlist + + At the end, we restore the painter state to what it was upon + entering the function and we return the bounding rectangle. + + \snippet tools/plugandpaintplugins/basictools/basictoolsplugin.cpp 4 + + When the user releases the mouse, we do nothing and return an + empty QRect. + + \section1 Implementation of the Shape Interface + + \snippet tools/plugandpaintplugins/basictools/basictoolsplugin.cpp 5 + + The plugin provides three shapes: \uicontrol{Circle}, \uicontrol{Star}, and + \uicontrol{Text...}. The three dots after \uicontrol{Text} are there because + the shape pops up a dialog asking for more information. We know + that the shape names will end up in a menu, so we include the + three dots in the shape name. + + A cleaner but more complicated design would have been to + distinguish between the internal shape name and the name used in + the user interface. + + \snippet tools/plugandpaintplugins/basictools/basictoolsplugin.cpp 6 + + The \c generateShape() creates a QPainterPath for the specified + shape. If the shape is \uicontrol{Text}, we pop up a QInputDialog to + let the user enter some text. + + \section1 Implementation of the Filter Interface + + \snippet tools/plugandpaintplugins/basictools/basictoolsplugin.cpp 7 + + The plugin provides three filters: \uicontrol{Invert Pixels}, \uicontrol{Swap + RGB}, and \uicontrol{Grayscale}. + + \snippet tools/plugandpaintplugins/basictools/basictoolsplugin.cpp 8 + + The \c filterImage() function takes a filter name and a QImage as + parameters and returns an altered QImage. The first thing we do + is to convert the image to a 32-bit RGB format, to ensure that + the algorithms will work as expected. For example, + QImage::invertPixels(), which is used to implement the + \uicontrol{Invert Pixels} filter, gives counterintuitive results for + 8-bit images, because they invert the indices into the color + table instead of inverting the color table's entries. + + \section1 Exporting the Plugin + + Whereas applications have a \c main() function as their entry + point, plugins need to contain exactly one occurrence of the + Q_EXPORT_PLUGIN2() macro to specify which class provides the + plugin: + + \snippet tools/plugandpaintplugins/basictools/basictoolsplugin.cpp 9 + + This line may appear in any \c .cpp file that is part of the + plugin's source code. + + \section1 The .pro File + + Here's the project file for building the Basic Tools plugin: + + \snippet tools/plugandpaintplugins/basictools/basictools.pro 0 + + The \c .pro file differs from typical \c .pro files in many + respects. First, it starts with a \c TEMPLATE entry specifying \c + lib. (The default template is \c app.) It also adds \c plugin to + the \c CONFIG variable. This is necessary on some platforms to + avoid generating symbolic links with version numbers in the file + name, which is appropriate for most dynamic libraries but not for + plugins. + + To make the plugin a static plugin, all that is required is to + specify \c static in addition to \c plugin. The + \l{plugandpaintplugins/extrafilters}{Extra Filters} plugin, + which is compiled as a dynamic plugin, doesn't specify \c static + in its \c .pro file. + + The \c INCLUDEPATH variable sets the search paths for global + headers (i.e., header files included using \c{#include <...>}). + We add Qt's \c examples/tools directory (strictly speaking, + \c{examples/tools/plugandpaintplugins/basictools/../..}) to the + list, so that we can include \c <plugandpaint/interfaces.h>. + + The \c TARGET variable specifies which name we want to give the + target library. We use \c pnp_ as the prefix to show that the + plugin is designed to work with Plug & Paint. On Unix, \c lib is + also prepended to that name. On all platforms, a + platform-specific suffix is appended (e.g., \c .dll on Windows, + \c .a on Linux). + + The \c CONFIG() code at the end is necessary for this example + because the example is part of the Qt distribution and Qt can be + configured to be built simultaneously in debug and in release + modes. You don't need to for your own plugins. +*/ + +/*! + \example tools/plugandpaintplugins/extrafilters + \title Plug & Paint Extra Filters Example + + The Extra Filters example is a plugin for the + \l{plugandpaint}{Plug & Paint} example. It provides a set + of filters in addition to those provided by the + \l{plugandpaintplugins/basictools}{Basic Tools} plugin. + + Since the approach is identical to + \l{plugandpaintplugins/basictools}{Basic Tools}, we won't + review the code here. The only part of interest is the + \c .pro file, since Extra Filters is a dynamic plugin + (\l{plugandpaintplugins/basictools}{Basic Tools} is + linked statically into the Plug & Paint executable). + + Here's the project file for building the Extra Filters plugin: + + \snippet tools/plugandpaintplugins/extrafilters/extrafilters.pro 0 + + The \c .pro file differs from typical \c .pro files in many + respects. First, it starts with a \c TEMPLATE entry specifying \c + lib. (The default template is \c app.) It also adds \c plugin to + the \c CONFIG variable. This is necessary on some platforms to + avoid generating symbolic links with version numbers in the file + name, which is appropriate for most dynamic libraries but not for + plugins. + + The \c INCLUDEPATH variable sets the search paths for global + headers (i.e., header files included using \c{#include <...>}). + We add Qt's \c examples/tools directory (strictly speaking, + \c{examples/tools/plugandpaintplugins/basictools/../..}) to the + list, so that we can include \c <plugandpaint/interfaces.h>. + + The \c TARGET variable specifies which name we want to give the + target library. We use \c pnp_ as the prefix to show that the + plugin is designed to work with Plug & Paint. On Unix, \c lib is + also prepended to that name. On all platforms, a + platform-specific suffix is appended (e.g., \c .dll on Windows, + \c .so on Linux). + + The \c DESTDIR variable specifies where we want to install the + plugin. We put it in Plug & Paint's \c plugins subdirectory, + since that's where the application looks for dynamic plugins. + + The \c CONFIG() code at the end is necessary for this example + because the example is part of the Qt distribution and Qt can be + configured to be built simultaneously in debug and in release + modes. You don't need to for your own plugins. +*/ diff --git a/examples/widgets/doc/src/regexp.qdoc b/examples/widgets/doc/src/regexp.qdoc new file mode 100644 index 0000000000..a2c7b53ed2 --- /dev/null +++ b/examples/widgets/doc/src/regexp.qdoc @@ -0,0 +1,37 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example tools/regexp + \title Regular Expressions Example + + The Regular Expressions (RegExp) example shows how regular expressions in Qt are + applied to text by providing an environment in which new regular expressions can be + created and tested on custom text strings. + + \image regexp-example.png +*/ diff --git a/examples/widgets/doc/src/settingseditor.qdoc b/examples/widgets/doc/src/settingseditor.qdoc new file mode 100644 index 0000000000..6d295bb928 --- /dev/null +++ b/examples/widgets/doc/src/settingseditor.qdoc @@ -0,0 +1,37 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example tools/settingseditor + \title Settings Editor Example + + The Settings Editor example shows how Qt's standard settings support is used in an + application by providing an editor that enables the user to view the settings for + installed applications, and modify those that can be edited. + + \image settingseditor-example.png +*/ diff --git a/examples/widgets/doc/src/styleplugin.qdoc b/examples/widgets/doc/src/styleplugin.qdoc new file mode 100644 index 0000000000..42f35f2141 --- /dev/null +++ b/examples/widgets/doc/src/styleplugin.qdoc @@ -0,0 +1,137 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example tools/styleplugin + \title Style Plugin Example + + This example shows how to create a plugin that extends Qt with a new + GUI look and feel. + + \image stylepluginexample.png + + A plugin in Qt is a class stored in a shared library that can be + loaded by a QPluginLoader at run-time. When you create plugins in + Qt, they either extend a Qt application or Qt itself. Writing a + plugin that extends Qt itself is achieved by inheriting one of the + plugin \l{Plugin Classes}{base classes}, reimplementing functions + from that class, and adding a macro. In this example we extend Qt + by adding a new GUI look and feel (i.e., making a new QStyle + available). A high-level introduction to plugins is given in the + plugin \l{How to Create Qt Plugins}{overview document}. + + Plugins that provide new styles inherit the QStylePlugin base + class. Style plugins are loaded by Qt and made available through + QStyleFactory; we will look at this later. We have implemented \c + SimpleStylePlugin, which provides \c SimpleStyle. The new style + contributes to widget styling by drawing button backgrounds in + red - not a major contribution, but it still makes a new style. + + The new style is platform agnostic in the sense that it is not + based on any specific style implementation, but uses QProxyStyle + to merely tweak the looks in the current application style that + defaults to the native system style. + + \note On some platforms, the native style will prevent the button + from having a red background. In this case, try to run the example + in another style (e.g., fusion). + + We test the plugin with \c StyleWindow, in which we display a + QPushButton. The \c SimpleStyle and \c StyleWindow classes do not + contain any plugin specific functionality and their implementations + are trivial; we will therefore leap past them and head on to the \c + SimpleStylePlugin and the \c main() function. After we have looked + at that, we examine the plugin's profile. + + + \section1 SimpleStylePlugin Class Definition + + \c SimpleStylePlugin inherits QStylePlugin and is the plugin + class. + + \snippet tools/styleplugin/plugin/simplestyleplugin.h 0 + + \c keys() returns a list of style names that this plugin can + create, while \c create() takes such a string and returns the + QStyle corresponding to the key. Both functions are pure virtual + functions reimplemented from QStylePlugin. When an application + requests an instance of the \c SimpleStyle style, which this + plugin creates, Qt will create it with this plugin. + + + \section1 SimpleStylePlugin Class Implementation + + Here is the implementation of \c keys(): + + \snippet tools/styleplugin/plugin/simplestyleplugin.cpp 0 + + Since this plugin only supports one style, we return a QStringList + with the class name of that style. + + Here is the \c create() function: + + \snippet tools/styleplugin/plugin/simplestyleplugin.cpp 1 + + Note that the key for style plugins are case insensitive. + The case sensitivity varies from plugin to plugin, so you need to + check this when implementing new plugins. + + \section1 The \c main() function + + \snippet tools/styleplugin/stylewindow/main.cpp 0 + + Qt loads the available style plugins when the QApplication object + is initialized. The QStyleFactory class knows about all styles and + produces them with \l{QStyleFactory::}{create()} (it is a + wrapper around all the style plugins). + + \section1 The Simple Style Plugin Profile + + The \c SimpleStylePlugin lives in its own directory and have + its own profile: + + \snippet tools/styleplugin/plugin/plugin.pro 0 + + In the plugin profile we need to set the lib template as we are + building a shared library instead of an executable. We must also + set the config to plugin. We set the library to be stored in the + styles folder under stylewindow because this is a path in which Qt + will search for style plugins. + + \section1 Related articles and examples + + In addition to the plugin \l{How to Create Qt Plugins}{overview + document}, we have other examples and articles that concern + plugins. + + In the \l{Echo Plugin Example}{echo plugin example} we show how to + implement plugins that extends Qt applications rather than Qt + itself, which is the case with the style plugin of this example. + The \l{Plug & Paint Example}{plug & paint} example shows how to + implement a static plugin as well as being a more involved example + on plugins that extend applications. +*/ diff --git a/examples/widgets/doc/src/treemodelcompleter.qdoc b/examples/widgets/doc/src/treemodelcompleter.qdoc new file mode 100644 index 0000000000..c6b7768976 --- /dev/null +++ b/examples/widgets/doc/src/treemodelcompleter.qdoc @@ -0,0 +1,171 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example tools/treemodelcompleter + \title Tree Model Completer Example + + The Tree Model Completer example shows how to provide completion + facilities for a hierarchical model, using a period as the separator + to access Child, GrandChild and GrandGrandChild level objects. + + \image treemodelcompleter-example.png + + Similar to the \l{Completer Example}, we provide QComboBox objects to + enable selection for completion mode and case sensitivity, as well as + a QCheckBox for wrap completions. + + \section1 The Resource File + + The contents of the TreeModelCompleter is read from \e treemodel.txt. + This file is embedded within the \e treemodelcompleter.qrc resource file, + which contains the following: + + \quotefile tools/treemodelcompleter/treemodelcompleter.qrc + + \section1 TreeModelCompleter Class Definition + + The \c TreeModelCompleter is a subclass of QCompleter with two + constructors - one with \a parent as an argument and another with + \a parent and \a model as arguments. + + \snippet tools/treemodelcompleter/treemodelcompleter.h 0 + + The class reimplements the protected functions + \l{QCompleter::splitPath()}{splitPath()} and + \l{QCompleter::pathFromIndex()}{pathFromIndex()} to suit a tree model. + For more information on customizing QCompleter to suit tree models, refer + to \l{QCompleter#Handling Tree Models}{Handling Tree Models}. + + \c TreeModelCompleter also has a separator property which is declared + using the Q_PROPERTY() macro. The separator has READ and WRITE attributes + and the corresponding functions \c separator() and \c setSeparator(). For + more information on Q_PROPERTY(), refer to \l{Qt's Property System}. + + \section1 TreeModelCompleter Class Implementation + + The first constructor constructs a \c TreeModelCompleter object with a + parent while the second constructor constructs an object with a parent + and a QAbstractItemModel, \a model. + + \snippet tools/treemodelcompleter/treemodelcompleter.cpp 0 + \codeline + \snippet tools/treemodelcompleter/treemodelcompleter.cpp 1 + + The \c separator() function is a getter function that returns the + separator string. + + \snippet tools/treemodelcompleter/treemodelcompleter.cpp 2 + + As mentioned earlier, the \c splitPath() function is reimplemented because + the default implementation is more suited to QDirModel or list models. In + order for QCompleter to split the path into a list of strings that are + matched at each level, we split it using QString::split() with \c sep as its + separator. + + \snippet tools/treemodelcompleter/treemodelcompleter.cpp 3 + + The \c pathFromIndex() function returns data for the completionRole() for a + tree model. This function is reimplemented as its default implementation is + more suitable for list models. If there is no separator, we use + \l{QCompleter}'s default implementation, otherwise we use the + \l{QStringList::prepend()}{prepend()} function to navigate upwards and + accumulate the data. The function then returns a QStringList, \c dataList, + using a separator to join objects of different levels. + + \snippet tools/treemodelcompleter/treemodelcompleter.cpp 4 + + \section1 MainWindow Class Definition + + The \c MainWindow class is a subclass of QMainWindow and implements five + custom slots: \c about(), \c changeCase(), \c changeMode(), + \c highlight(), and \c updateContentsLabel(). + + \snippet tools/treemodelcompleter/mainwindow.h 0 + + In addition, the class has two private functions, \c createMenu() and + \c modelFromFile(), as well as private instances of QTreeView, QComboBox, + QLabel, \c TreeModelCompleter and QLineEdit. + + \snippet tools/treemodelcompleter/mainwindow.h 1 + + \section1 MainWindow Class Implementation + + The \c{MainWindow}'s constructor creates a \c MainWindow object with a + parent and initializes the \c completer and \c lineEdit. The + \c createMenu() function is invoked to set up the "File" menu and "Help" + menu. The \c{completer}'s model is set to the QAbstractItemModel obtained + from \c modelFromFile(), and the \l{QCompleter::highlighted()} + {highlighted()} signal is connected to \c{MainWindow}'s \c highlight() + slot. + + \snippet tools/treemodelcompleter/mainwindow.cpp 0 + + The QLabel objects \c modelLabel, \c modeLabel and \c caseLabel are + instantiated. Also, the QComboBox objects, \c modeCombo and \c caseCombo, + are instantiated and populated. By default, the \c{completer}'s mode is + "Filtered Popup" and the case is insensitive. + + \snippet tools/treemodelcompleter/mainwindow.cpp 1 + \codeline + \snippet tools/treemodelcompleter/mainwindow.cpp 2 + + We use a QGridLayout to place all the objects in the \c MainWindow. + + \snippet tools/treemodelcompleter/mainwindow.cpp 3 + + The \c createMenu() function sets up the QAction objects required and + adds them to the "File" menu and "Help" menu. The + \l{QAction::triggered()}{triggered()} signals from these actions are + connected to their respective slots. + + \snippet tools/treemodelcompleter/mainwindow.cpp 4 + + The \c changeMode() function accepts an \a index corresponding to the + user's choice of completion mode and changes the \c{completer}'s mode + accordingly. + + \snippet tools/treemodelcompleter/mainwindow.cpp 5 + + The \c about() function provides a brief description on the Tree Model + Completer example. + + \snippet tools/treemodelcompleter/mainwindow.cpp 6 + + The \c changeCase() function alternates between \l{Qt::CaseSensitive} + {Case Sensitive} and \l{Qt::CaseInsensitive}{Case Insensitive} modes, + depending on the value of \a cs. + + \snippet tools/treemodelcompleter/mainwindow.cpp 7 + + \section1 \c main() Function + + The \c main() function instantiates \c MainWindow and invokes the + \l{QWidget::show()}{show()} function to display it. + + \snippet tools/treemodelcompleter/main.cpp 0 +*/ diff --git a/examples/widgets/doc/src/undo.qdoc b/examples/widgets/doc/src/undo.qdoc new file mode 100644 index 0000000000..03920e44dd --- /dev/null +++ b/examples/widgets/doc/src/undo.qdoc @@ -0,0 +1,43 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example tools/undo + \title Undo Framework + + This example shows Qt's undo framework in action. + + \image undodemo.png + + Qt's undo framework is an implementation of the Command + pattern, which provides advanced undo/redo functionality. + + To show the abilities of the framework, we have implemented a + small diagram application in which the diagram items are geometric + primitives. You can edit the diagram in the following ways: add, + move, change the color of, and delete the items. +*/ diff --git a/examples/widgets/doc/src/undoframework.qdoc b/examples/widgets/doc/src/undoframework.qdoc new file mode 100644 index 0000000000..849b2e6143 --- /dev/null +++ b/examples/widgets/doc/src/undoframework.qdoc @@ -0,0 +1,291 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example tools/undoframework + \title Undo Framework Example + + This example shows how to implement undo/redo functionality + with the Qt undo framework. + + \image undoframeworkexample.png The Undo Diagram Example + + In the Qt undo framework, all actions that the user performs are + implemented in classes that inherit QUndoCommand. An undo command + class knows how to both \l{QUndoCommand::}{redo()} - or just do + the first time - and \l{QUndoCommand::}{undo()} an action. For + each action the user performs, a command is placed on a + QUndoStack. Since the stack contains all commands executed + (stacked in chronological order) on the document, it can roll the + state of the document backwards and forwards by undoing and redoing + its commands. See the \l{Overview of Qt's Undo Framework}{overview + document} for a high-level introduction to the undo framework. + + The undo example implements a simple diagram application. It is + possible to add and delete items, which are either box or + rectangular shaped, and move the items by dragging them with the + mouse. The undo stack is shown in a QUndoView, which is a list in + which the commands are shown as list items. Undo and redo are + available through the edit menu. The user can also select a command + from the undo view. + + We use the \l{Graphics View Framework}{graphics view + framework} to implement the diagram. We only treat the related + code briefly as the framework has examples of its own (e.g., the + \l{Diagram Scene Example}). + + The example consists of the following classes: + + \list + \li \c MainWindow is the main window and arranges the + example's widgets. It creates the commands based + on user input and keeps them on the command stack. + \li \c AddCommand adds an item to the scene. + \li \c DeleteCommand deletes an item from the scene. + \li \c MoveCommand when an item is moved the MoveCommand keeps record + of the start and stop positions of the move, and it + moves the item according to these when \c redo() and \c undo() + is called. + \li \c DiagramScene inherits QGraphicsScene and + emits signals for the \c MoveComands when an item is moved. + \li \c DiagramItem inherits QGraphicsPolygonItem and represents + an item in the diagram. + \endlist + + \section1 MainWindow Class Definition + + \snippet tools/undoframework/mainwindow.h 0 + + The \c MainWindow class maintains the undo stack, i.e., it creates + \l{QUndoCommand}s and pushes and pops them from the stack when it + receives the \c triggered() signal from \c undoAction and \c + redoAction. + + \section1 MainWindow Class Implementation + + We will start with a look at the constructor: + + \snippet tools/undoframework/mainwindow.cpp 0 + + In the constructor, we set up the DiagramScene and QGraphicsView. + + Here is the \c createUndoView() function: + + \snippet tools/undoframework/mainwindow.cpp 1 + + The QUndoView is a widget that display the text, which is set with + the \l{QUndoCommand::}{setText()} function, for each QUndoCommand + in the undo stack in a list. + + Here is the \c createActions() function: + + \snippet tools/undoframework/mainwindow.cpp 2 + \codeline + \snippet tools/undoframework/mainwindow.cpp 3 + \dots + \snippet tools/undoframework/mainwindow.cpp 5 + + The \c createActions() function sets up all the examples actions + in the manner shown above. The + \l{QUndoStack::}{createUndoAction()} and + \l{QUndoStack::}{createRedoAction()} helps us crate actions that + are disabled and enabled based on the state of the stack. Also, + the text of the action will be updated automatically based on the + \l{QUndoCommand::}{text()} of the undo commands. For the other + actions we have implemented slots in the \c MainWindow class. + + Here is the \c createMenus() function: + + \snippet tools/undoframework/mainwindow.cpp 6 + + \dots + \snippet tools/undoframework/mainwindow.cpp 7 + \dots + \snippet tools/undoframework/mainwindow.cpp 8 + + We have to use the QMenu \c aboutToShow() and \c aboutToHide() + signals since we only want \c deleteAction to be enabled when we + have selected an item. + + Here is the \c itemMoved() slot: + + \snippet tools/undoframework/mainwindow.cpp 9 + + We simply push a MoveCommand on the stack, which calls \c redo() + on it. + + Here is the \c deleteItem() slot: + + \snippet tools/undoframework/mainwindow.cpp 10 + + An item must be selected to be deleted. We need to check if it is + selected as the \c deleteAction may be enabled even if an item is + not selected. This can happen as we do not catch a signal or event + when an item is selected. + + Here is the \c itemMenuAboutToShow() and itemMenuAboutToHide() slots: + + \snippet tools/undoframework/mainwindow.cpp 11 + \codeline + \snippet tools/undoframework/mainwindow.cpp 12 + + We implement \c itemMenuAboutToShow() and \c itemMenuAboutToHide() + to get a dynamic item menu. These slots are connected to the + \l{QMenu::}{aboutToShow()} and \l{QMenu::}{aboutToHide()} signals. + We need this to disable or enable the \c deleteAction. + + Here is the \c addBox() slot: + + \snippet tools/undoframework/mainwindow.cpp 13 + + The \c addBox() function creates an AddCommand and pushes it on + the undo stack. + + Here is the \c addTriangle() sot: + + \snippet tools/undoframework/mainwindow.cpp 14 + + The \c addTriangle() function creates an AddCommand and pushes it + on the undo stack. + + Here is the implementation of \c about(): + + \snippet tools/undoframework/mainwindow.cpp 15 + + The about slot is triggered by the \c aboutAction and displays an + about box for the example. + + \section1 AddCommand Class Definition + + \snippet tools/undoframework/commands.h 2 + + The \c AddCommand class adds DiagramItem graphics items to the + DiagramScene. + + \section1 AddCommand Class Implementation + + We start with the constructor: + + \snippet tools/undoframework/commands.cpp 7 + + We first create the DiagramItem to add to the DiagramScene. The + \l{QUndoCommand::}{setText()} function let us set a QString that + describes the command. We use this to get custom messages in the + QUndoView and in the menu of the main window. + + \snippet tools/undoframework/commands.cpp 8 + + \c undo() removes the item from the scene. + + \snippet tools/undoframework/commands.cpp 9 + + We set the position of the item as we do not do this in the + constructor. + + \section1 DeleteCommand Class Definition + + \snippet tools/undoframework/commands.h 1 + + The DeleteCommand class implements the functionality to remove an + item from the scene. + + \section1 DeleteCommand Class Implementation + + \snippet tools/undoframework/commands.cpp 4 + + We know that there must be one selected item as it is not possible + to create a DeleteCommand unless the item to be deleted is + selected and that only one item can be selected at any time. + The item must be unselected if it is inserted back into the + scene. + + \snippet tools/undoframework/commands.cpp 5 + + The item is simply reinserted into the scene. + + \snippet tools/undoframework/commands.cpp 6 + + The item is removed from the scene. + + \section1 MoveCommand Class Definition + + \snippet tools/undoframework/commands.h 0 + + The \l{QUndoCommand::}{mergeWith()} is reimplemented to make + consecutive moves of an item one MoveCommand, i.e, the item will + be moved back to the start position of the first move. + + \section1 MoveCommand Class Implementation + + + The constructor of MoveCommand looks like this: + + \snippet tools/undoframework/commands.cpp 0 + + We save both the old and new positions for undo and redo + respectively. + + \snippet tools/undoframework/commands.cpp 2 + + We simply set the items old position and update the scene. + + \snippet tools/undoframework/commands.cpp 3 + + We set the item to its new position. + + \snippet tools/undoframework/commands.cpp 1 + + Whenever a MoveCommand is created, this function is called to + check if it should be merged with the previous command. It is the + previous command object that is kept on the stack. The function + returns true if the command is merged; otherwise false. + + We first check whether it is the same item that has been moved + twice, in which case we merge the commands. We update the position + of the item so that it will take the last position in the move + sequence when undone. + + \section1 DiagramScene Class Definition + + \snippet tools/undoframework/diagramscene.h 0 + + The DiagramScene implements the functionality to move a + DiagramItem with the mouse. It emits a signal when a move is + completed. This is caught by the \c MainWindow, which makes + MoveCommands. We do not examine the implementation of DiagramScene + as it only deals with graphics framework issues. + + \section1 The \c main() Function + + The \c main() function of the program looks like this: + + \snippet tools/undoframework/main.cpp 0 + + We draw a grid in the background of the DiagramScene, so we use a + resource file. The rest of the function creates the \c MainWindow and + shows it as a top level window. +*/ diff --git a/examples/widgets/tools/codecs/codecs.desktop b/examples/widgets/tools/codecs/codecs.desktop new file mode 100644 index 0000000000..bba62207f8 --- /dev/null +++ b/examples/widgets/tools/codecs/codecs.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=Codecs +Exec=/opt/usr/bin/codecs +Icon=codecs +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tools/codecs/codecs.pro b/examples/widgets/tools/codecs/codecs.pro new file mode 100644 index 0000000000..377dfaa8c9 --- /dev/null +++ b/examples/widgets/tools/codecs/codecs.pro @@ -0,0 +1,15 @@ +HEADERS += mainwindow.h \ + previewform.h +SOURCES += main.cpp \ + mainwindow.cpp \ + previewform.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tools/codecs +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS encodedfiles codecs.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/tools/codecs +INSTALLS += target sources + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tools/codecs/encodedfiles/iso-8859-1.txt b/examples/widgets/tools/codecs/encodedfiles/iso-8859-1.txt new file mode 100644 index 0000000000..d7fcacae66 --- /dev/null +++ b/examples/widgets/tools/codecs/encodedfiles/iso-8859-1.txt @@ -0,0 +1,6 @@ +Paulo Coelho: O Gênio e as Rosas +Anna Hallström, Urban Östberg: Svår svenska +Darrell Huff: How to Lie with Statistics +Franz Kafka: Das Schloß +Walter Moers: Die 13½ Leben des Käpt'n Blaubär +Dag Solstad: Forsøk på å beskrive det ugjennomtrengelige diff --git a/examples/widgets/tools/codecs/encodedfiles/iso-8859-15.txt b/examples/widgets/tools/codecs/encodedfiles/iso-8859-15.txt new file mode 100644 index 0000000000..be2d83c98d --- /dev/null +++ b/examples/widgets/tools/codecs/encodedfiles/iso-8859-15.txt @@ -0,0 +1,8 @@ +Paulo Coelho: O Gênio e as Rosas +Jean-Pierre Coffe: À table en famille avec 15 ¤ par jour +Anna Hallström, Urban Östberg: Svår svenska +Darrell Huff: How to Lie with Statistics +Franz Kafka: Das Schloß +Helena Lehecková: T¨ekkiä suomalaisille +Arthur Rimbaud: ¼uvres complètes +Dag Solstad: Forsøk på å beskrive det ugjennomtrengelige diff --git a/examples/widgets/tools/codecs/encodedfiles/utf-16.txt b/examples/widgets/tools/codecs/encodedfiles/utf-16.txt Binary files differnew file mode 100644 index 0000000000..371f06f7fa --- /dev/null +++ b/examples/widgets/tools/codecs/encodedfiles/utf-16.txt diff --git a/examples/widgets/tools/codecs/encodedfiles/utf-16be.txt b/examples/widgets/tools/codecs/encodedfiles/utf-16be.txt Binary files differnew file mode 100644 index 0000000000..d8ae6428bc --- /dev/null +++ b/examples/widgets/tools/codecs/encodedfiles/utf-16be.txt diff --git a/examples/widgets/tools/codecs/encodedfiles/utf-16le.txt b/examples/widgets/tools/codecs/encodedfiles/utf-16le.txt Binary files differnew file mode 100644 index 0000000000..3779264c7c --- /dev/null +++ b/examples/widgets/tools/codecs/encodedfiles/utf-16le.txt diff --git a/examples/widgets/tools/codecs/encodedfiles/utf-8.txt b/examples/widgets/tools/codecs/encodedfiles/utf-8.txt new file mode 100644 index 0000000000..a5e4ae6fa7 --- /dev/null +++ b/examples/widgets/tools/codecs/encodedfiles/utf-8.txt @@ -0,0 +1,6 @@ +SprÃ¥k: Norsk +Γλώσσα: Ελληνικά +Язык: РуÑÑкий +언어 : 한êµì–´ +言語: 日本語 +Langage : Français diff --git a/examples/widgets/tools/codecs/main.cpp b/examples/widgets/tools/codecs/main.cpp new file mode 100644 index 0000000000..f03543e449 --- /dev/null +++ b/examples/widgets/tools/codecs/main.cpp @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + MainWindow mainWin; + mainWin.show(); + return app.exec(); +} diff --git a/examples/widgets/tools/codecs/mainwindow.cpp b/examples/widgets/tools/codecs/mainwindow.cpp new file mode 100644 index 0000000000..4009e30720 --- /dev/null +++ b/examples/widgets/tools/codecs/mainwindow.cpp @@ -0,0 +1,202 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include "mainwindow.h" +#include "previewform.h" + +MainWindow::MainWindow() +{ + textEdit = new QTextEdit; + textEdit->setLineWrapMode(QTextEdit::NoWrap); + setCentralWidget(textEdit); + + findCodecs(); + + previewForm = new PreviewForm(this); + previewForm->setCodecList(codecs); + + createActions(); + createMenus(); + + setWindowTitle(tr("Codecs")); + resize(500, 400); +} + +void MainWindow::open() +{ + QString fileName = QFileDialog::getOpenFileName(this); + if (!fileName.isEmpty()) { + QFile file(fileName); + if (!file.open(QFile::ReadOnly)) { + QMessageBox::warning(this, tr("Codecs"), + tr("Cannot read file %1:\n%2") + .arg(fileName) + .arg(file.errorString())); + return; + } + + QByteArray data = file.readAll(); + + previewForm->setEncodedData(data); + if (previewForm->exec()) + textEdit->setPlainText(previewForm->decodedString()); + } +} + +void MainWindow::save() +{ + QString fileName = QFileDialog::getSaveFileName(this); + if (!fileName.isEmpty()) { + QFile file(fileName); + if (!file.open(QFile::WriteOnly | QFile::Text)) { + QMessageBox::warning(this, tr("Codecs"), + tr("Cannot write file %1:\n%2") + .arg(fileName) + .arg(file.errorString())); + return; + } + + QAction *action = qobject_cast<QAction *>(sender()); + QByteArray codecName = action->data().toByteArray(); + + QTextStream out(&file); + out.setCodec(codecName.constData()); + out << textEdit->toPlainText(); + } +} + +void MainWindow::about() +{ + QMessageBox::about(this, tr("About Codecs"), + tr("The <b>Codecs</b> example demonstrates how to read and write " + "files using various encodings.")); +} + +void MainWindow::aboutToShowSaveAsMenu() +{ + QString currentText = textEdit->toPlainText(); + + foreach (QAction *action, saveAsActs) { + QByteArray codecName = action->data().toByteArray(); + QTextCodec *codec = QTextCodec::codecForName(codecName); + action->setVisible(codec && codec->canEncode(currentText)); + } +} + +void MainWindow::findCodecs() +{ + QMap<QString, QTextCodec *> codecMap; + QRegExp iso8859RegExp("ISO[- ]8859-([0-9]+).*"); + + foreach (int mib, QTextCodec::availableMibs()) { + QTextCodec *codec = QTextCodec::codecForMib(mib); + + QString sortKey = codec->name().toUpper(); + int rank; + + if (sortKey.startsWith("UTF-8")) { + rank = 1; + } else if (sortKey.startsWith("UTF-16")) { + rank = 2; + } else if (iso8859RegExp.exactMatch(sortKey)) { + if (iso8859RegExp.cap(1).size() == 1) + rank = 3; + else + rank = 4; + } else { + rank = 5; + } + sortKey.prepend(QChar('0' + rank)); + + codecMap.insert(sortKey, codec); + } + codecs = codecMap.values(); +} + +void MainWindow::createActions() +{ + openAct = new QAction(tr("&Open..."), this); + openAct->setShortcuts(QKeySequence::Open); + connect(openAct, SIGNAL(triggered()), this, SLOT(open())); + + foreach (QTextCodec *codec, codecs) { + QString text = tr("%1...").arg(QString(codec->name())); + + QAction *action = new QAction(text, this); + action->setData(codec->name()); + connect(action, SIGNAL(triggered()), this, SLOT(save())); + saveAsActs.append(action); + } + + exitAct = new QAction(tr("E&xit"), this); + exitAct->setShortcuts(QKeySequence::Quit); + connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); + + aboutAct = new QAction(tr("&About"), this); + connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); + + aboutQtAct = new QAction(tr("About &Qt"), this); + connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); +} + +void MainWindow::createMenus() +{ + saveAsMenu = new QMenu(tr("&Save As"), this); + foreach (QAction *action, saveAsActs) + saveAsMenu->addAction(action); + connect(saveAsMenu, SIGNAL(aboutToShow()), + this, SLOT(aboutToShowSaveAsMenu())); + + fileMenu = new QMenu(tr("&File"), this); + fileMenu->addAction(openAct); + fileMenu->addMenu(saveAsMenu); + fileMenu->addSeparator(); + fileMenu->addAction(exitAct); + + helpMenu = new QMenu(tr("&Help"), this); + helpMenu->addAction(aboutAct); + helpMenu->addAction(aboutQtAct); + + menuBar()->addMenu(fileMenu); + menuBar()->addSeparator(); + menuBar()->addMenu(helpMenu); +} diff --git a/examples/widgets/tools/codecs/mainwindow.h b/examples/widgets/tools/codecs/mainwindow.h new file mode 100644 index 0000000000..1e04c3bf65 --- /dev/null +++ b/examples/widgets/tools/codecs/mainwindow.h @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QList> +#include <QMainWindow> + +QT_BEGIN_NAMESPACE +class QAction; +class QMenu; +class QTextCodec; +class QTextEdit; +QT_END_NAMESPACE +class PreviewForm; + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +private slots: + void open(); + void save(); + void about(); + void aboutToShowSaveAsMenu(); + +private: + void findCodecs(); + void createActions(); + void createMenus(); + + QTextEdit *textEdit; + PreviewForm *previewForm; + QList<QTextCodec *> codecs; + + QMenu *fileMenu; + QMenu *helpMenu; + QMenu *saveAsMenu; + QAction *openAct; + QList<QAction *> saveAsActs; + QAction *exitAct; + QAction *aboutAct; + QAction *aboutQtAct; +}; + +#endif diff --git a/examples/widgets/tools/codecs/previewform.cpp b/examples/widgets/tools/codecs/previewform.cpp new file mode 100644 index 0000000000..dc518555d2 --- /dev/null +++ b/examples/widgets/tools/codecs/previewform.cpp @@ -0,0 +1,101 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include "previewform.h" + +PreviewForm::PreviewForm(QWidget *parent) + : QDialog(parent) +{ + encodingComboBox = new QComboBox; + + encodingLabel = new QLabel(tr("&Encoding:")); + encodingLabel->setBuddy(encodingComboBox); + + textEdit = new QTextEdit; + textEdit->setLineWrapMode(QTextEdit::NoWrap); + textEdit->setReadOnly(true); + + buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok + | QDialogButtonBox::Cancel); + + connect(encodingComboBox, SIGNAL(activated(int)), + this, SLOT(updateTextEdit())); + connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); + connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); + + QGridLayout *mainLayout = new QGridLayout; + mainLayout->addWidget(encodingLabel, 0, 0); + mainLayout->addWidget(encodingComboBox, 0, 1); + mainLayout->addWidget(textEdit, 1, 0, 1, 2); + mainLayout->addWidget(buttonBox, 2, 0, 1, 2); + setLayout(mainLayout); + + setWindowTitle(tr("Choose Encoding")); + resize(400, 300); +} + +void PreviewForm::setCodecList(const QList<QTextCodec *> &list) +{ + encodingComboBox->clear(); + foreach (QTextCodec *codec, list) + encodingComboBox->addItem(codec->name(), codec->mibEnum()); +} + +void PreviewForm::setEncodedData(const QByteArray &data) +{ + encodedData = data; + updateTextEdit(); +} + +void PreviewForm::updateTextEdit() +{ + int mib = encodingComboBox->itemData( + encodingComboBox->currentIndex()).toInt(); + QTextCodec *codec = QTextCodec::codecForMib(mib); + + QTextStream in(&encodedData); + in.setAutoDetectUnicode(false); + in.setCodec(codec); + decodedStr = in.readAll(); + + textEdit->setPlainText(decodedStr); +} diff --git a/examples/widgets/tools/codecs/previewform.h b/examples/widgets/tools/codecs/previewform.h new file mode 100644 index 0000000000..d8e2838e8e --- /dev/null +++ b/examples/widgets/tools/codecs/previewform.h @@ -0,0 +1,79 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PREVIEWFORM_H +#define PREVIEWFORM_H + +#include <QDialog> +#include <QList> + +QT_BEGIN_NAMESPACE +class QComboBox; +class QDialogButtonBox; +class QLabel; +class QTextCodec; +class QTextEdit; +QT_END_NAMESPACE + +class PreviewForm : public QDialog +{ + Q_OBJECT + +public: + PreviewForm(QWidget *parent = 0); + + void setCodecList(const QList<QTextCodec *> &list); + void setEncodedData(const QByteArray &data); + QString decodedString() const { return decodedStr; } + +private slots: + void updateTextEdit(); + +private: + QByteArray encodedData; + QString decodedStr; + + QComboBox *encodingComboBox; + QLabel *encodingLabel; + QTextEdit *textEdit; + QDialogButtonBox *buttonBox; +}; + +#endif diff --git a/examples/widgets/tools/completer/completer.desktop b/examples/widgets/tools/completer/completer.desktop new file mode 100644 index 0000000000..f7e2d155d8 --- /dev/null +++ b/examples/widgets/tools/completer/completer.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=Completer +Exec=/opt/usr/bin/completer +Icon=completer +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tools/completer/completer.pro b/examples/widgets/tools/completer/completer.pro new file mode 100644 index 0000000000..a735b7ceae --- /dev/null +++ b/examples/widgets/tools/completer/completer.pro @@ -0,0 +1,16 @@ +HEADERS = fsmodel.h \ + mainwindow.h +SOURCES = fsmodel.cpp \ + main.cpp \ + mainwindow.cpp +RESOURCES = completer.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tools/completer +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS completer.pro resources +sources.path = $$[QT_INSTALL_EXAMPLES]/tools/completer +INSTALLS += target sources + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tools/completer/completer.qrc b/examples/widgets/tools/completer/completer.qrc new file mode 100644 index 0000000000..4f57e1a824 --- /dev/null +++ b/examples/widgets/tools/completer/completer.qrc @@ -0,0 +1,6 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix="/"> + <file>resources/countries.txt</file> + <file>resources/wordlist.txt</file> +</qresource> +</RCC> diff --git a/examples/widgets/tools/completer/fsmodel.cpp b/examples/widgets/tools/completer/fsmodel.cpp new file mode 100644 index 0000000000..5967f3d3a7 --- /dev/null +++ b/examples/widgets/tools/completer/fsmodel.cpp @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "fsmodel.h" + +//! [0] +FileSystemModel::FileSystemModel(QObject *parent) + : QFileSystemModel(parent) +{ +} +//! [0] + +//! [1] +QVariant FileSystemModel::data(const QModelIndex &index, int role) const +{ + if (role == Qt::DisplayRole && index.column() == 0) { + QString path = QDir::toNativeSeparators(filePath(index)); + if (path.endsWith(QDir::separator())) + path.chop(1); + return path; + } + + return QFileSystemModel::data(index, role); +} + +//! [1] diff --git a/examples/widgets/tools/completer/fsmodel.h b/examples/widgets/tools/completer/fsmodel.h new file mode 100644 index 0000000000..8b7b8eb6bc --- /dev/null +++ b/examples/widgets/tools/completer/fsmodel.h @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef FILESYSTEMMODEL_H +#define FILESYSTEMMODEL_H + +#include <QFileSystemModel> + +// With a QFileSystemModel, set on a view, you will see "Program Files" in the view +// But with this model, you will see "C:\Program Files" in the view. +// We acheive this, by having the data() return the entire file path for +// the display role. Note that the Qt::EditRole over which the QCompleter +// looks for matches is left unchanged +//! [0] +class FileSystemModel : public QFileSystemModel +{ +public: + FileSystemModel(QObject *parent = 0); + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; +}; +//! [0] + +#endif diff --git a/examples/widgets/tools/completer/main.cpp b/examples/widgets/tools/completer/main.cpp new file mode 100644 index 0000000000..3f80523eb7 --- /dev/null +++ b/examples/widgets/tools/completer/main.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> +#include "mainwindow.h" + +//! [0] +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(completer); + + QApplication app(argc, argv); + MainWindow window; + window.show(); + return app.exec(); +} +//! [0] diff --git a/examples/widgets/tools/completer/mainwindow.cpp b/examples/widgets/tools/completer/mainwindow.cpp new file mode 100644 index 0000000000..97b5b1b88f --- /dev/null +++ b/examples/widgets/tools/completer/mainwindow.cpp @@ -0,0 +1,281 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> +#include "fsmodel.h" +#include "mainwindow.h" + +//! [0] +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent), completer(0), lineEdit(0) +{ + createMenu(); + + QWidget *centralWidget = new QWidget; + + QLabel *modelLabel = new QLabel; + modelLabel->setText(tr("Model")); + + modelCombo = new QComboBox; + modelCombo->addItem(tr("QFileSytemModel")); + modelCombo->addItem(tr("QFileSytemModel that shows full path")); + modelCombo->addItem(tr("Country list")); + modelCombo->addItem(tr("Word list")); + modelCombo->setCurrentIndex(0); + + QLabel *modeLabel = new QLabel; + modeLabel->setText(tr("Completion Mode")); + modeCombo = new QComboBox; + modeCombo->addItem(tr("Inline")); + modeCombo->addItem(tr("Filtered Popup")); + modeCombo->addItem(tr("Unfiltered Popup")); + modeCombo->setCurrentIndex(1); + + QLabel *caseLabel = new QLabel; + caseLabel->setText(tr("Case Sensitivity")); + caseCombo = new QComboBox; + caseCombo->addItem(tr("Case Insensitive")); + caseCombo->addItem(tr("Case Sensitive")); + caseCombo->setCurrentIndex(0); +//! [0] + +//! [1] + QLabel *maxVisibleLabel = new QLabel; + maxVisibleLabel->setText(tr("Max Visible Items")); + maxVisibleSpinBox = new QSpinBox; + maxVisibleSpinBox->setRange(3,25); + maxVisibleSpinBox->setValue(10); + + wrapCheckBox = new QCheckBox; + wrapCheckBox->setText(tr("Wrap around completions")); + wrapCheckBox->setChecked(true); +//! [1] + +//! [2] + contentsLabel = new QLabel; + contentsLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); + + connect(modelCombo, SIGNAL(activated(int)), this, SLOT(changeModel())); + connect(modeCombo, SIGNAL(activated(int)), this, SLOT(changeMode(int))); + connect(caseCombo, SIGNAL(activated(int)), this, SLOT(changeCase(int))); + connect(maxVisibleSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeMaxVisible(int))); +//! [2] + +//! [3] + lineEdit = new QLineEdit; + + QGridLayout *layout = new QGridLayout; + layout->addWidget(modelLabel, 0, 0); layout->addWidget(modelCombo, 0, 1); + layout->addWidget(modeLabel, 1, 0); layout->addWidget(modeCombo, 1, 1); + layout->addWidget(caseLabel, 2, 0); layout->addWidget(caseCombo, 2, 1); + layout->addWidget(maxVisibleLabel, 3, 0); layout->addWidget(maxVisibleSpinBox, 3, 1); + layout->addWidget(wrapCheckBox, 4, 0); + layout->addWidget(contentsLabel, 5, 0, 1, 2); + layout->addWidget(lineEdit, 6, 0, 1, 2); + centralWidget->setLayout(layout); + setCentralWidget(centralWidget); + + changeModel(); + + setWindowTitle(tr("Completer")); + lineEdit->setFocus(); +} +//! [3] + +//! [4] +void MainWindow::createMenu() +{ + QAction *exitAction = new QAction(tr("Exit"), this); + QAction *aboutAct = new QAction(tr("About"), this); + QAction *aboutQtAct = new QAction(tr("About Qt"), this); + + connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit())); + connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); + connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); + + QMenu* fileMenu = menuBar()->addMenu(tr("File")); + fileMenu->addAction(exitAction); + + QMenu* helpMenu = menuBar()->addMenu(tr("About")); + helpMenu->addAction(aboutAct); + helpMenu->addAction(aboutQtAct); +} +//! [4] + +//! [5] +QAbstractItemModel *MainWindow::modelFromFile(const QString& fileName) +{ + QFile file(fileName); + if (!file.open(QFile::ReadOnly)) + return new QStringListModel(completer); +//! [5] + +//! [6] +#ifndef QT_NO_CURSOR + QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); +#endif + QStringList words; + + while (!file.atEnd()) { + QByteArray line = file.readLine(); + if (!line.isEmpty()) + words << line.trimmed(); + } + +#ifndef QT_NO_CURSOR + QApplication::restoreOverrideCursor(); +#endif +//! [6] + +//! [7] + if (!fileName.contains(QLatin1String("countries.txt"))) + return new QStringListModel(words, completer); +//! [7] + + // The last two chars of the countries.txt file indicate the country + // symbol. We put that in column 2 of a standard item model +//! [8] + QStandardItemModel *m = new QStandardItemModel(words.count(), 2, completer); +//! [8] //! [9] + for (int i = 0; i < words.count(); ++i) { + QModelIndex countryIdx = m->index(i, 0); + QModelIndex symbolIdx = m->index(i, 1); + QString country = words[i].mid(0, words[i].length() - 2).trimmed(); + QString symbol = words[i].right(2); + m->setData(countryIdx, country); + m->setData(symbolIdx, symbol); + } + + return m; +} +//! [9] + +//! [10] +void MainWindow::changeMode(int index) +{ + QCompleter::CompletionMode mode; + if (index == 0) + mode = QCompleter::InlineCompletion; + else if (index == 1) + mode = QCompleter::PopupCompletion; + else + mode = QCompleter::UnfilteredPopupCompletion; + + completer->setCompletionMode(mode); +} +//! [10] + +void MainWindow::changeCase(int cs) +{ + completer->setCaseSensitivity(cs ? Qt::CaseSensitive : Qt::CaseInsensitive); +} + +//! [11] +void MainWindow::changeModel() +{ + delete completer; + completer = new QCompleter(this); + completer->setMaxVisibleItems(maxVisibleSpinBox->value()); + + switch (modelCombo->currentIndex()) { + default: + case 0: + { // Unsorted QFileSystemModel + QFileSystemModel *fsModel = new QFileSystemModel(completer); + fsModel->setRootPath(""); + completer->setModel(fsModel); + contentsLabel->setText(tr("Enter file path")); + } + break; +//! [11] //! [12] + case 1: + { // FileSystemModel that shows full paths + FileSystemModel *fsModel = new FileSystemModel(completer); + completer->setModel(fsModel); + fsModel->setRootPath(""); + contentsLabel->setText(tr("Enter file path")); + } + break; +//! [12] //! [13] + case 2: + { // Country List + completer->setModel(modelFromFile(":/resources/countries.txt")); + QTreeView *treeView = new QTreeView; + completer->setPopup(treeView); + treeView->setRootIsDecorated(false); + treeView->header()->hide(); + treeView->header()->setStretchLastSection(false); + treeView->header()->setSectionResizeMode(0, QHeaderView::Stretch); + treeView->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents); + contentsLabel->setText(tr("Enter name of your country")); + } + break; +//! [13] //! [14] + case 3: + { // Word list + completer->setModel(modelFromFile(":/resources/wordlist.txt")); + completer->setModelSorting(QCompleter::CaseInsensitivelySortedModel); + contentsLabel->setText(tr("Enter a word")); + } + break; + } + + changeMode(modeCombo->currentIndex()); + changeCase(caseCombo->currentIndex()); + completer->setWrapAround(wrapCheckBox->isChecked()); + lineEdit->setCompleter(completer); + connect(wrapCheckBox, SIGNAL(clicked(bool)), completer, SLOT(setWrapAround(bool))); +} +//! [14] + +//! [15] +void MainWindow::changeMaxVisible(int max) +{ + completer->setMaxVisibleItems(max); +} +//! [15] + +//! [16] +void MainWindow::about() +{ + QMessageBox::about(this, tr("About"), tr("This example demonstrates the " + "different features of the QCompleter class.")); +} +//! [16] diff --git a/examples/widgets/tools/completer/mainwindow.h b/examples/widgets/tools/completer/mainwindow.h new file mode 100644 index 0000000000..b3c57f1a51 --- /dev/null +++ b/examples/widgets/tools/completer/mainwindow.h @@ -0,0 +1,89 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> + +QT_BEGIN_NAMESPACE +class QAbstractItemModel; +class QComboBox; +class QCompleter; +class QLabel; +class QLineEdit; +class QProgressBar; +class QCheckBox; +class QSpinBox; +QT_END_NAMESPACE + +//! [0] +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = 0); + +private slots: + void about(); + void changeCase(int); + void changeMode(int); + void changeModel(); + void changeMaxVisible(int); +//! [0] + +//! [1] +private: + void createMenu(); + QAbstractItemModel *modelFromFile(const QString& fileName); + + QComboBox *caseCombo; + QComboBox *modeCombo; + QComboBox *modelCombo; + QSpinBox *maxVisibleSpinBox; + QCheckBox *wrapCheckBox; + QCompleter *completer; + QLabel *contentsLabel; + QLineEdit *lineEdit; +}; +//! [1] + +#endif // MAINWINDOW_H diff --git a/examples/widgets/tools/completer/resources/countries.txt b/examples/widgets/tools/completer/resources/countries.txt new file mode 100644 index 0000000000..5854fbc977 --- /dev/null +++ b/examples/widgets/tools/completer/resources/countries.txt @@ -0,0 +1,241 @@ +Afghanistan AF +Albania AL +Algeria DZ +American Samoa AS +Andorra AD +Angola AO +Anguilla AI +Antarctica AQ +Antigua And Barbuda AG +Argentina AR +Armenia AM +Aruba AW +Australia AU +Austria AT +Azerbaijan AZ +Bahamas BS +Bahrain BH +Bangladesh BD +Barbados BB +Belarus BY +Belgium BE +Belize BZ +Benin BJ +Bermuda BM +Bhutan BT +Bolivia BO +Bosnia And Herzegowina BA +Botswana BW +Bouvet Island BV +Brazil BR +British Indian Ocean Territory IO +British Virgin Islands VG +Brunei Darussalam BN +Bulgaria BG +Burkina Faso BF +Burundi BI +Cambodia KH +Cameroon CM +Canada CA +Cape Verde CV +Cayman Islands KY +Central African Republic CF +Chad TD +Chile CL +China CN +Christmas Island CX +Cocos Islands CC +Colombia CO +Comoros KM +Cook Islands CK +Costa Rica CR +Croatia HR +Cuba CU +Cyprus CY +Czech Republic CZ +Democratic Republic Of Congo CD +Democratic Republic Of Korea KP +Denmark DK +Djibouti DJ +Dominica DM +Dominican Republic DO +EastTimor TL +Ecuador EC +Egypt EG +El Salvador SV +Equatorial Guinea GQ +Eritrea ER +Estonia EE +Ethiopia ET +Falkland Islands FK +Faroe Islands FO +Fiji FJ +Finland FI +France FR +French Guiana GF +French Polynesia PF +French Southern Territories TF +Gabon GA +Gambia GM +Georgia GE +Germany DE +Ghana GH +Gibraltar GI +Greece GR +Greenland GL +Grenada GD +Guadeloupe GP +Guam GU +Guatemala GT +Guinea GN +Guinea Bissau GW +Guyana GY +Haiti HT +Heard And McDonald Islands HM +Honduras HN +Hong Kong HK +Hungary HU +Iceland IS +India IN +Indonesia ID +Iran IR +Iraq IQ +Ireland IE +Israel IL +Italy IT +Ivory Coast CI +Jamaica JM +Japan JP +Jordan JO +Kazakhstan KZ +Kenya KE +Kiribati KI +Kuwait KW +Kyrgyzstan KG +Lao LA +Latvia LV +Lebanon LB +Lesotho LS +Liberia LR +Libyan Arab Jamahiriya LY +Liechtenstein LI +Lithuania LT +Luxembourg LU +Macau MO +Macedonia MK +Madagascar MG +Malawi MW +Malaysia MY +Maldives MV +Mali ML +Malta MT +Marshall Islands MH +Martinique MQ +Mauritania MR +Mauritius MU +Mayotte YT +Metropolitan France FX +Mexico MX +Micronesia FM +Moldova MD +Monaco MC +Mongolia MN +Montserrat MS +Morocco MA +Mozambique MZ +Myanmar MM +Namibia NA +Nauru NR +Nepal NP +Netherlands NL +Netherlands Antilles AN +New Caledonia NC +New Zealand NZ +Nicaragua NI +Niger NE +Nigeria NG +Niue NU +Norfolk Island NF +Northern Mariana Islands MP +Norway NO +Oman OM +Pakistan PK +Palau PW +Palestinian Territory PS +Panama PA +Papua New Guinea PG +Paraguay PY +Peoples Republic Of Congo CG +Peru PE +Philippines PH +Pitcairn PN +Poland PL +Portugal PT +Puerto Rico PR +Qatar QA +Republic Of Korea KR +Reunion RE +Romania RO +Russian Federation RU +Rwanda RW +Saint Kitts And Nevis KN +Samoa WS +San Marino SM +Sao Tome And Principe ST +Saudi Arabia SA +Senegal SN +Serbia And Montenegro CS +Seychelles SC +Sierra Leone SL +Singapore SG +Slovakia SK +Slovenia SI +Solomon Islands SB +Somalia SO +South Africa ZA +South Georgia And The South Sandwich Islands GS +Spain ES +Sri Lanka LK +St Helena SH +St Lucia LC +St Pierre And Miquelon PM +St Vincent And The Grenadines VC +Sudan SD +Suriname SR +Svalbard And Jan Mayen Islands SJ +Swaziland SZ +Sweden SE +Switzerland CH +Syrian Arab Republic SY +Taiwan TW +Tajikistan TJ +Tanzania TZ +Thailand TH +Togo TG +Tokelau TK +Tonga TO +Trinidad And Tobago TT +Tunisia TN +Turkey TR +Turkmenistan TM +Turks And Caicos Islands TC +Tuvalu TV +US Virgin Islands VI +Uganda UG +Ukraine UA +United Arab Emirates AE +United Kingdom GB +United States US +United States Minor Outlying Islands UM +Uruguay UY +Uzbekistan UZ +Vanuatu VU +Vatican City State VA +Venezuela VE +Viet Nam VN +Wallis And Futuna Islands WF +Western Sahara EH +Yemen YE +Yugoslavia YU +Zambia ZM +Zimbabwe ZW diff --git a/examples/widgets/tools/completer/resources/wordlist.txt b/examples/widgets/tools/completer/resources/wordlist.txt new file mode 100644 index 0000000000..1f56e36d47 --- /dev/null +++ b/examples/widgets/tools/completer/resources/wordlist.txt @@ -0,0 +1,1485 @@ +A4 +able +about +above +absence +absolutely +abstract +access +according +accumulated +achieve +achieving +activity +acts +actual +actually +add +added +adding +addition +additionally +additions +addresses +adjust +adjustments +advanced +advice +after +afterwards +again +agenda +aim +algorithm +all +allocated +allow +allowed +allowing +allows +along +alpha +already +also +alternative +alternatively +although +American +an +and +announced +annoy +another +answer +answers +any +anything +anyway +apart +API +appear +appears +appendices +appendix +appends +application +applications +apply +approach +approaches +appropriate +Arabic +arbitrary +are +areas +ARGB +argument +arguments +around +arrangements +arrive +arrived +Arthur +article +articles +as +asked +aspects +assume +at +attachment +attempt +attempting +attend +attendees +attributes +authors +auto-detect +auto-detecting +availability +available +avoid +away +back +background +backgrounds +bandwidth +bandwidths +Barcelona +base +based +basic +basically +basics +be +because +been +before +behave +behavior +behind +being +below +benefits +Berkeley +between +bit +bits +bitwise +black +blended +blending +blends +block +blue +BMP +body +bold +booking +bool +Boston +both +bottom +box +boxes +braces +break +breaks +broad +browsers +buffer +buffers +build +builds +built +bundled +burdens +busy +but +by +bypass +bypassing +bytes +calendar +call +called +calling +calls +Cambridge +can +canonical +canonicalised +cap +capabilities +capacity +caption +card +care +case +cast +catch +causing +centre +certain +challenges +chance +change +changes +changing +channel +channels +chapter +char +chart +charts +check +checks +Chicago +chit-chat +chosen +chunk +circle +citation +city +claim +class +classes +clause +clauses +clear +clearing +client +clients +close +closed +co-author +code +colon +color +color-coded +colorize +colorizer +colors +colour +column +columns +combine +combined +combines +combining +comes +command +commands +comment +common +communicate +community +compiled +complement +complete +completely +completeness +completes +completion +complex +compliant +component +components +compositing +composition +compression +computation +computer +concepts +conclusion +concurrent +configurable +congested +congestion +connect +connected +connection +connections +cons +consider +consisting +consists +construct +constructed +constructing +constructor +constructs +consume +contact +contain +containing +contains +contents +contents +continue +continued +contributors +control +controlled +controller +controller +controlling +controls +controls +conventions +cook +cooperation +copy +copyright +core +cores +corollary +correct +correctly +corresponding +could +couple +coworkers +CPU +create +creates +creating +cross-platform +crucial +cultures +current +currently +custom +customized +cut +data +database +datasets +datum +day +days +deal +dealing +decide +decouple +decoupled +deeply +def +default +define +defines +definition +definitions +delegate +delete +demo +demonstrate +demonstrations +deployed +describe +describes +design +desktop +desktops +destination +destinations +destructor +details +detect +determine +determines +developer +developers +development +developments +device +devices +diagram +dialogs +dictionary +did +difference +differences +different +differs +digital +direct +direct +directions +directly +directory +discuss +display +displaying +displays +distribute +distribution +disturbing +divide +do +documentation +documents +does +done +down +downLimit +download +downloaded +downloading +draw +drawing +drawn +drop +drop-in +Duff +during +DVD +dynamic +dynamically +dynamics +each +earlier +easily +editing +editors +education +effect +effectively +effects +either +ellipse +ellipses +elliptical +else +email +e-mail +embedded +emit +emits +empty +enable +enables +encapsulates +enclose +end +endnote +endnotes +end-user +engineer +engineering +English +enjoys +enough +ensure +ensures +entails +enter +entire +entitled +entries +entry +environment +erases +error +errors +especially +established +etc. +Europe +even +evenly +event +events +eventually +every +everyone +example +examples +except +exception +excessive +exclusive +existing +exists +expand +expected +expense +export +exposes +extend +extended +extending +extensible +extension +external +extra +extract +faces +facilities +factory +fade +failure +fairness +falls +false +family +fashion +fast +faster +features +February +feedback +feel +fetch +fetching +few +fields +figure +figures +file +file name +files +filled +filler +final +finally +find +fine +finish +finished +first +flow +fly +focus +followed +following +font +foot +footnote +footnotes +for +form +format +formats +format-specific +forms +formula +formulas +forum +found +framework +France +from +front +full +fully +function +functionality +function-based +functions +future +gain +games +gap +general +generic +Germany +get +gill +give +given +gives +giving +global +go +goes +got +gradient +gradient-filled +graph +graphical +graphics +gray +great +greatly +green +grey +group +growing +gui +GUI +hack +had +half +Hamburg +hand +handing +handle +handler +handlers +handles +handling +happens +hardware +harmonica +has +have +having +he +head +header +headers +hear +height +help +helper +here +high +high-complexity +high-level +highlight +highly +hints +his +hold +holder +holding +hole +home +horizontal +host +hosting +hours +Houston +how +however +huge +hyphen +ID +idea +ideally +if +Illinois +illustrate +illustrated +illustrates +image +images +impact +implement +implementation +implementations +implemented +implementing +implements +import +important +improvements +in +include +included +includes +including +inclusion +incoming +inconvenient +indent +index +indicate +inexpensively +influence +info +information +ingenious +inherits +initial +initially +inner +input +inspired +install +installs +instance +instead +instructs +integer +integers +integration +intended +intensive +interest +interests +interface +interfaces +interfere +internal +interval +into +intriguing +intro +introduce +introduced +invalid +invented +inverse +invocation +involved +Irish +irregular +is +ISO +ISPs +issue +issues +it +items +iterate +its +itself +job +join +JPEG +jungle +just +Karaoke +keep +keeping +key +kind +king +known +label +labels +landscape +language +large +last +later +latest +lawn +layout +lead +leader +leaders +lean +leaves +leaving +left +lemma +length +less +let +level +levels +libraries +library +lies +like +likely +limit +limiting +limits +line +linear +lines +linewidth +link +linked +linking +Linux +list +lists +little +lives +load +loading +loads +located +location +locations +logo +long +longer +look +looked +looking +looks +lookup +lookups +loop +lout +low +lower-case +low-level +macro +made +magazine +magic +main +major +make +makes +making +manage +managed +managing +mandatory +manipulate +manipulation +manner +many +map +maps +March +margin +mask +masking +Massachusetts +match +mathematical +maximum +may +means +meet +member +members +memory +merged +method +might +milliseconds +minimize +minimum +minor +mix +MNG +mode +model +models +modes +modified +module +moments +monitored +monthly +more +most +mostly +move +much +multitude +Munich +must +name +named +names +necessary +need +needed +needs +network +new +news +newsletter +next +nicely +no +none +non-Qt +non-trivial +non-zero +normally +Norway +not +note +notes +nothing +notifies +notify +now +null +number +numbered +numbering +numbers +Nydalen +object +objects +obtain +obtaining +odd +of +off +office +offset +offsets +often +old +on +once +one +ones +online +only +on-screen +onto +opacity +opaque +open +opens +operates +operating +operation +operations +operator +opportunity +opposed +optimize +option +optionally +options +or +OR +order +ordinary +original +originally +Oslo +other +otherwise +our +out +outer +outgoing +output +outside +over +overcoming +overrides +overview +own +owner +owners +pace +Pacific +package +packages +packets +page +pages +paint +painted +painting +paragraph +paragraphs +parameters +parent +Paris +parse +part +partial +partially +particular +partners +parts +party +passed +passing +patches +path +pattern +patterns +pause +PDF +peek +pending +perform +performed +performs +permission +permutations +personal +pick +pie +pipe +pixel +pixels +place +places +planning +platform-independent +platforms +play +players +playing +please +pleasing +plugin +plugin-enabled +plugins +plus +PNG +pointer +pop +popular +populates +porter +Porter +portrait +possibility +possible +potential +potentially +power +powers +preceded +presence +present +prevent +prevents +previous +previously +primarily +primary +primitives +printed +printing +prior +probably +problem +process +processes +processing +produce +produced +produces +products +programming +programs +project +projects +proof +proper +properly +properties +property +proposed +proposition +pros +protocols +proud +provide +provided +provides +provision +proxy +public +published +punch +punches +pursuing +put +Qt +Qtopia +Qt-related +quality +quarter +quarterly +queried +queries +query +question +questions +radial +ragged +range +rate +rate-controlled +rates +rather +ratio +raw +read +reader +reading +reads +ready +real +realistic +really +real-time +received +recent +recognized +recommended +recompile +rectangle +rectangular +red +redistribute +reducing +reference +references +reflect +regardless +regional +register +registered +registration +reimplement +reimplementation +reimplemented +release +released +releases +reliable +rely +remains +remember +removed +removes +repeatedly +replace +report +represent +represented +represents +reproduced +requested +require +required +requires +resetting +resides +resolve +respective +respectively +response +rest +restart +result +resulting +resume +return +returning +returns +reuse +revealing +rich +riches +right +roadshow +role +Roman +round-trip +router +routers +rule +run +runner +runners +running +runs +run-time +sake +sales +salespeople +same +sample +San Jose +save +saved +saves +say +scalable +scale +scanline +scanline-level +scene +scenes +schedule +school +script +seam +seamless +search +searches +searching +second +seconds +section +sections +security +see +seen +selection +seminar +seminars +sending +separated +separates +series +service +services +set +setting +setup +seven +seventh +several +shade +shadow +shape +shares +shaves +shine +shorter +should +show +shown +shows +side +signal +signals +signature +significantly +similar +similarily +similarly +simple +simplest +simplified +simplifying +simply +simulate +simultaneous +since +single +size +sizes +slightly +slope +slot +slowing +small +smaller +so +socket +sockets +software +solutions +solved +some +soon +sort +sorting +source +sources +south +space +special +specialized +specific +specifically +specification +specifications +specified +specify +speed +spend +split +SQL +standard +standards +start +starts +starve +state +states +static +steady +stealing +step +still +stop +stopwatch +store +stored +stores +straightforward +stream +string +strings +structure +structured +strut +style +styles +styling +subclass +subclassing +subdirectory +subsection +subsections +succeeds +success +successful +successfully +such +suggestion +suggestions +suitable +support +supported +supports +suppose +supposes +sure +switch +switches +symbol +symbols +synonyms +system +systems +table +tables +tags +take +takes +tap +target +targets +TCP +team +technically-focused +technique +technology +template +templates +Texas +text +than +that +the +their +them +then +theorem +there +these +they +thickness +thin +third +this +those +three +throttling +through +tightly +time +timer +times +tiny +tips +title +titles +to +together +too +top +torrent +total +tour +trademarks +traffic +training +transaction +transfer +transferred +transferring +transfers +translucency +translucent +transparency +transparent +travel +traveled +true +try +turn +twice +two +type +typed +typical +typically +unable +under +underlying +understanding +undoes +unique +united +unlike +unpack +unsigned +until +untouched +up +update +updated +updates +upload +uploading +uploads +up-to-date +us +use +used +useful +user +user-friendly +users +uses +using +usual +usually +valid +value +values +variable +variation +variety +various +vector +venturing +verifying +versa +version +versions +vertical +very +via +vice +virtual +visual +void +VP +waiting +Wales +want +wants +was +way +ways +we +web +website +well +were +what +when +whenever +where +whether +which +while +whistle +white +who +whole +why +widget +widgets +width +widths +will +window +windows +wish +with +without +word +words +work +working +works +would +wrap +wrapper +write +writes +writing +writings +written +X +X11 +XOR +year +years +yes +you +your +zero diff --git a/examples/widgets/tools/customcompleter/customcompleter.desktop b/examples/widgets/tools/customcompleter/customcompleter.desktop new file mode 100644 index 0000000000..bbc21112ad --- /dev/null +++ b/examples/widgets/tools/customcompleter/customcompleter.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=Custom Completer +Exec=/opt/usr/bin/customcompleter +Icon=customcompleter +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tools/customcompleter/customcompleter.pro b/examples/widgets/tools/customcompleter/customcompleter.pro new file mode 100644 index 0000000000..b61cb510e9 --- /dev/null +++ b/examples/widgets/tools/customcompleter/customcompleter.pro @@ -0,0 +1,16 @@ +HEADERS = mainwindow.h \ + textedit.h +SOURCES = main.cpp \ + mainwindow.cpp \ + textedit.cpp +RESOURCES = customcompleter.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tools/customcompleter +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS customcompleter.pro resources +sources.path = $$[QT_INSTALL_EXAMPLES]/tools/customcompleter +INSTALLS += target sources + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tools/customcompleter/customcompleter.qrc b/examples/widgets/tools/customcompleter/customcompleter.qrc new file mode 100644 index 0000000000..d7da1bf9a6 --- /dev/null +++ b/examples/widgets/tools/customcompleter/customcompleter.qrc @@ -0,0 +1,5 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix="/"> + <file>resources/wordlist.txt</file> +</qresource> +</RCC> diff --git a/examples/widgets/tools/customcompleter/main.cpp b/examples/widgets/tools/customcompleter/main.cpp new file mode 100644 index 0000000000..893c73026d --- /dev/null +++ b/examples/widgets/tools/customcompleter/main.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> +#include "mainwindow.h" + +//! [0] +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(customcompleter); + + QApplication app(argc, argv); + MainWindow window; + window.show(); + return app.exec(); +} +//! [0] diff --git a/examples/widgets/tools/customcompleter/mainwindow.cpp b/examples/widgets/tools/customcompleter/mainwindow.cpp new file mode 100644 index 0000000000..702176eb1e --- /dev/null +++ b/examples/widgets/tools/customcompleter/mainwindow.cpp @@ -0,0 +1,117 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> +#include "mainwindow.h" +#include "textedit.h" + +//! [0] +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent), completer(0) +{ + createMenu(); + + completingTextEdit = new TextEdit; + completer = new QCompleter(this); + completer->setModel(modelFromFile(":/resources/wordlist.txt")); + completer->setModelSorting(QCompleter::CaseInsensitivelySortedModel); + completer->setCaseSensitivity(Qt::CaseInsensitive); + completer->setWrapAround(false); + completingTextEdit->setCompleter(completer); + + setCentralWidget(completingTextEdit); + resize(500, 300); + setWindowTitle(tr("Completer")); +} +//! [0] + +//! [1] +void MainWindow::createMenu() +{ + QAction *exitAction = new QAction(tr("Exit"), this); + QAction *aboutAct = new QAction(tr("About"), this); + QAction *aboutQtAct = new QAction(tr("About Qt"), this); + + connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit())); + connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); + connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); + + QMenu* fileMenu = menuBar()->addMenu(tr("File")); + fileMenu->addAction(exitAction); + + QMenu* helpMenu = menuBar()->addMenu(tr("About")); + helpMenu->addAction(aboutAct); + helpMenu->addAction(aboutQtAct); +} +//! [1] + +//! [2] +QAbstractItemModel *MainWindow::modelFromFile(const QString& fileName) +{ + QFile file(fileName); + if (!file.open(QFile::ReadOnly)) + return new QStringListModel(completer); + +#ifndef QT_NO_CURSOR + QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); +#endif + QStringList words; + + while (!file.atEnd()) { + QByteArray line = file.readLine(); + if (!line.isEmpty()) + words << line.trimmed(); + } + +#ifndef QT_NO_CURSOR + QApplication::restoreOverrideCursor(); +#endif + return new QStringListModel(words, completer); +} +//! [2] + +//! [3] +void MainWindow::about() +{ + QMessageBox::about(this, tr("About"), tr("This example demonstrates the " + "different features of the QCompleter class.")); +} +//! [3] + diff --git a/examples/widgets/tools/customcompleter/mainwindow.h b/examples/widgets/tools/customcompleter/mainwindow.h new file mode 100644 index 0000000000..2dae198a49 --- /dev/null +++ b/examples/widgets/tools/customcompleter/mainwindow.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> + +QT_BEGIN_NAMESPACE +class QAbstractItemModel; +class QComboBox; +class QCompleter; +class QLabel; +class QLineEdit; +class QProgressBar; +QT_END_NAMESPACE +class TextEdit; + +//! [0] +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = 0); + +private slots: + void about(); + +private: + void createMenu(); + QAbstractItemModel *modelFromFile(const QString& fileName); + + QCompleter *completer; + TextEdit *completingTextEdit; +}; +//! [0] + +#endif // MAINWINDOW_H diff --git a/examples/widgets/tools/customcompleter/resources/wordlist.txt b/examples/widgets/tools/customcompleter/resources/wordlist.txt new file mode 100644 index 0000000000..f8b581a405 --- /dev/null +++ b/examples/widgets/tools/customcompleter/resources/wordlist.txt @@ -0,0 +1,1454 @@ +A4 +able +about +above +absence +absolutely +abstract +access +according +accumulated +achieve +achieving +activity +acts +actual +actually +add +added +adding +addition +additionally +additions +addresses +adjust +adjustments +advanced +advice +after +afterwards +again +agenda +aim +algorithm +all +allocated +allow +allowed +allowing +allows +along +alpha +already +also +alternative +alternatively +although +American +an +and +announced +annoy +another +answer +answers +any +anything +anyway +apart +API +appear +appears +appendices +appendix +appends +application +applications +apply +approach +approaches +appropriate +Arabic +arbitrary +are +areas +ARGB +argument +arguments +around +arrangements +arrive +arrived +Arthur +article +articles +as +asked +aspects +assume +at +attachment +attempt +attempting +attend +attendees +attributes +authors +availability +available +avoid +away +back +background +backgrounds +bandwidth +bandwidths +Barcelona +base +based +basic +basically +basics +be +because +been +before +behave +behavior +behind +being +below +benefits +Berkeley +between +bit +bits +bitwise +black +blended +blending +blends +block +blue +BMP +body +bold +booking +bool +Boston +both +bottom +box +boxes +braces +break +breaks +broad +browsers +buffer +buffers +build +builds +built +bundled +burdens +busy +but +by +bypass +bypassing +bytes +calendar +call +called +calling +calls +Cambridge +can +canonical +canonicalised +cap +capabilities +capacity +caption +card +care +case +cast +catch +causing +centre +certain +challenges +chance +change +changes +changing +channel +channels +chapter +char +chart +charts +check +checks +Chicago +chosen +chunk +circle +citation +city +claim +class +classes +clause +clauses +clear +clearing +client +clients +close +closed +code +colon +color +colorize +colorizer +colors +colour +column +columns +combine +combined +combines +combining +comes +command +commands +comment +common +communicate +community +compiled +complement +complete +completely +completeness +completes +completion +complex +compliant +component +components +compositing +composition +compression +computation +computer +concepts +conclusion +concurrent +configurable +congested +congestion +connect +connected +connection +connections +cons +consider +consisting +consists +construct +constructed +constructing +constructor +constructs +consume +contact +contain +containing +contains +contents +contents +continue +continued +contributors +control +controlled +controller +controller +controlling +controls +controls +conventions +cook +cooperation +copy +copyright +core +cores +corollary +correct +correctly +corresponding +could +couple +coworkers +CPU +create +creates +creating +crucial +cultures +current +currently +custom +customized +cut +data +database +datasets +datum +day +days +deal +dealing +decide +decouple +decoupled +deeply +def +default +define +defines +definition +definitions +delegate +delete +demo +demonstrate +demonstrations +deployed +describe +describes +design +desktop +desktops +destination +destinations +destructor +details +detect +determine +determines +developer +developers +development +developments +device +devices +diagram +dialogs +dictionary +did +difference +differences +different +differs +digital +direct +direct +directions +directly +directory +discuss +display +displaying +displays +distribute +distribution +disturbing +divide +do +documentation +documents +does +done +down +downLimit +download +downloaded +downloading +draw +drawing +drawn +drop +Duff +during +DVD +dynamic +dynamically +dynamics +each +earlier +easily +editing +editors +education +effect +effectively +effects +either +ellipse +ellipses +elliptical +else +email +embedded +emit +emits +empty +enable +enables +encapsulates +enclose +end +endnote +endnotes +engineer +engineering +English +enjoys +enough +ensure +ensures +entails +enter +entire +entitled +entries +entry +environment +erases +error +errors +especially +established +etc. +Europe +even +evenly +event +events +eventually +every +everyone +example +examples +except +exception +excessive +exclusive +existing +exists +expand +expected +expense +export +exposes +extend +extended +extending +extensible +extension +external +extra +extract +faces +facilities +factory +fade +failure +fairness +falls +false +family +fashion +fast +faster +features +February +feedback +feel +fetch +fetching +few +fields +figure +figures +file +file name +files +filled +filler +final +finally +find +fine +finish +finished +first +flow +fly +focus +followed +following +font +foot +footnote +footnotes +for +form +format +formats +forms +formula +formulas +forum +found +framework +France +from +front +full +fully +function +functionality +functions +future +gain +games +gap +general +generic +Germany +get +gill +give +given +gives +giving +global +go +goes +got +gradient +graph +graphical +graphics +gray +great +greatly +green +grey +group +growing +gui +GUI +hack +had +half +Hamburg +hand +handing +handle +handler +handlers +handles +handling +happens +hardware +harmonica +has +have +having +he +head +header +headers +hear +height +help +helper +here +high +highlight +highly +hints +his +hold +holder +holding +hole +home +horizontal +host +hosting +hours +Houston +how +however +huge +hyphen +ID +idea +ideally +if +Illinois +illustrate +illustrated +illustrates +image +images +impact +implement +implementation +implementations +implemented +implementing +implements +import +important +improvements +in +include +included +includes +including +inclusion +incoming +inconvenient +indent +index +indicate +inexpensively +influence +info +information +ingenious +inherits +initial +initially +inner +input +inspired +install +installs +instance +instead +instructs +integer +integers +integration +intended +intensive +interest +interests +interface +interfaces +interfere +internal +interval +into +intriguing +intro +introduce +introduced +invalid +invented +inverse +invocation +involved +Irish +irregular +is +ISO +ISPs +issue +issues +it +items +iterate +its +itself +job +join +JPEG +jungle +just +Karaoke +keep +keeping +key +kind +king +known +label +labels +landscape +language +large +last +later +latest +lawn +layout +lead +leader +leaders +lean +leaves +leaving +left +lemma +length +less +let +level +levels +libraries +library +lies +like +likely +limit +limiting +limits +line +linear +lines +linewidth +link +linked +linking +Linux +list +lists +little +lives +load +loading +loads +located +location +locations +logo +long +longer +look +looked +looking +looks +lookup +lookups +loop +lout +low +macro +made +magazine +magic +main +major +make +makes +making +manage +managed +managing +mandatory +manipulate +manipulation +manner +many +map +maps +March +margin +mask +masking +Massachusetts +match +mathematical +maximum +may +means +meet +member +members +memory +merged +method +might +milliseconds +minimize +minimum +minor +mix +MNG +mode +model +models +modes +modified +module +moments +monitored +monthly +more +most +mostly +move +much +multitude +Munich +must +name +named +names +necessary +need +needed +needs +network +new +news +newsletter +next +nicely +no +none +normally +Norway +not +note +notes +nothing +notifies +notify +now +null +number +numbered +numbering +numbers +Nydalen +object +objects +obtain +obtaining +odd +of +off +office +offset +offsets +often +old +on +once +one +ones +online +only +onto +opacity +opaque +open +opens +operates +operating +operation +operations +operator +opportunity +opposed +optimize +option +optionally +options +or +OR +order +ordinary +original +originally +Oslo +other +otherwise +our +out +outer +outgoing +output +outside +over +overcoming +overrides +overview +own +owner +owners +pace +Pacific +package +packages +packets +page +pages +paint +painted +painting +paragraph +paragraphs +parameters +parent +Paris +parse +part +partial +partially +particular +partners +parts +party +passed +passing +patches +path +pattern +patterns +pause +PDF +peek +pending +perform +performed +performs +permission +permutations +personal +pick +pie +pipe +pixel +pixels +place +places +planning +platforms +play +players +playing +please +pleasing +plugin +plugins +plus +PNG +pointer +pop +popular +populates +porter +Porter +portrait +possibility +possible +potential +potentially +power +powers +preceded +presence +present +prevent +prevents +previous +previously +primarily +primary +primitives +printed +printing +prior +probably +problem +process +processes +processing +produce +produced +produces +products +programming +programs +project +projects +proof +proper +properly +properties +property +proposed +proposition +pros +protocols +proud +provide +provided +provides +provision +proxy +public +published +punch +punches +pursuing +put +Qt +Qtopia +quality +quarter +quarterly +queried +queries +query +question +questions +radial +ragged +range +rate +rates +rather +ratio +raw +read +reader +reading +reads +ready +real +realistic +really +received +recent +recognized +recommended +recompile +rectangle +rectangular +red +redistribute +reducing +reference +references +reflect +regardless +regional +register +registered +registration +reimplement +reimplementation +reimplemented +release +released +releases +reliable +rely +remains +remember +removed +removes +repeatedly +replace +report +represent +represented +represents +reproduced +requested +require +required +requires +resetting +resides +resolve +respective +respectively +response +rest +restart +result +resulting +resume +return +returning +returns +reuse +revealing +rich +riches +right +roadshow +role +Roman +router +routers +rule +run +runner +runners +running +runs +sake +sales +salespeople +same +sample +San Jose +save +saved +saves +say +scalable +scale +scanline +scene +scenes +schedule +school +script +seam +seamless +search +searches +searching +second +seconds +section +sections +security +see +seen +selection +seminar +seminars +sending +separated +separates +series +service +services +set +setting +setup +seven +seventh +several +shade +shadow +shape +shares +shaves +shine +shorter +should +show +shown +shows +side +signal +signals +signature +significantly +similar +similarily +similarly +simple +simplest +simplified +simplifying +simply +simulate +simultaneous +since +single +size +sizes +slightly +slope +slot +slowing +small +smaller +so +socket +sockets +software +solutions +solved +some +soon +sort +sorting +source +sources +south +space +special +specialized +specific +specifically +specification +specifications +specified +specify +speed +spend +split +SQL +standard +standards +start +starts +starve +state +states +static +steady +stealing +step +still +stop +stopwatch +store +stored +stores +straightforward +stream +string +strings +structure +structured +strut +style +styles +styling +subclass +subclassing +subdirectory +subsection +subsections +succeeds +success +successful +successfully +such +suggestion +suggestions +suitable +support +supported +supports +suppose +supposes +sure +switch +switches +symbol +symbols +synonyms +system +systems +table +tables +tags +take +takes +tap +target +targets +TCP +team +technique +technology +template +templates +Texas +text +than +that +the +their +them +then +theorem +there +these +they +thickness +thin +third +this +those +three +throttling +through +tightly +time +timer +times +tiny +tips +title +titles +to +together +too +top +torrent +total +tour +trademarks +traffic +training +transaction +transfer +transferred +transferring +transfers +translucency +translucent +transparency +transparent +travel +traveled +true +try +turn +twice +two +type +typed +typical +typically +unable +under +underlying +understanding +undoes +unique +united +unlike +unpack +unsigned +until +untouched +up +update +updated +updates +upload +uploading +uploads +us +use +used +useful +user +users +uses +using +usual +usually +valid +value +values +variable +variation +variety +various +vector +venturing +verifying +versa +version +versions +vertical +very +via +vice +virtual +visual +void +VP +waiting +Wales +want +wants +was +way +ways +we +web +website +well +were +what +when +whenever +where +whether +which +while +whistle +white +who +whole +why +widget +widgets +width +widths +will +window +windows +wish +with +without +word +words +work +working +works +would +wrap +wrapper +write +writes +writing +writings +written +X +X11 +XOR +year +years +yes +you +your +zero diff --git a/examples/widgets/tools/customcompleter/textedit.cpp b/examples/widgets/tools/customcompleter/textedit.cpp new file mode 100644 index 0000000000..65054e736d --- /dev/null +++ b/examples/widgets/tools/customcompleter/textedit.cpp @@ -0,0 +1,173 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "textedit.h" +#include <QCompleter> +#include <QKeyEvent> +#include <QAbstractItemView> +#include <QtDebug> +#include <QApplication> +#include <QModelIndex> +#include <QAbstractItemModel> +#include <QScrollBar> + +//! [0] +TextEdit::TextEdit(QWidget *parent) +: QTextEdit(parent), c(0) +{ + setPlainText(tr("This TextEdit provides autocompletions for words that have more than" + " 3 characters. You can trigger autocompletion using ") + + QKeySequence("Ctrl+E").toString(QKeySequence::NativeText)); +} +//! [0] + +//! [1] +TextEdit::~TextEdit() +{ +} +//! [1] + +//! [2] +void TextEdit::setCompleter(QCompleter *completer) +{ + if (c) + QObject::disconnect(c, 0, this, 0); + + c = completer; + + if (!c) + return; + + c->setWidget(this); + c->setCompletionMode(QCompleter::PopupCompletion); + c->setCaseSensitivity(Qt::CaseInsensitive); + QObject::connect(c, SIGNAL(activated(QString)), + this, SLOT(insertCompletion(QString))); +} +//! [2] + +//! [3] +QCompleter *TextEdit::completer() const +{ + return c; +} +//! [3] + +//! [4] +void TextEdit::insertCompletion(const QString& completion) +{ + if (c->widget() != this) + return; + QTextCursor tc = textCursor(); + int extra = completion.length() - c->completionPrefix().length(); + tc.movePosition(QTextCursor::Left); + tc.movePosition(QTextCursor::EndOfWord); + tc.insertText(completion.right(extra)); + setTextCursor(tc); +} +//! [4] + +//! [5] +QString TextEdit::textUnderCursor() const +{ + QTextCursor tc = textCursor(); + tc.select(QTextCursor::WordUnderCursor); + return tc.selectedText(); +} +//! [5] + +//! [6] +void TextEdit::focusInEvent(QFocusEvent *e) +{ + if (c) + c->setWidget(this); + QTextEdit::focusInEvent(e); +} +//! [6] + +//! [7] +void TextEdit::keyPressEvent(QKeyEvent *e) +{ + if (c && c->popup()->isVisible()) { + // The following keys are forwarded by the completer to the widget + switch (e->key()) { + case Qt::Key_Enter: + case Qt::Key_Return: + case Qt::Key_Escape: + case Qt::Key_Tab: + case Qt::Key_Backtab: + e->ignore(); + return; // let the completer do default behavior + default: + break; + } + } + + bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_E); // CTRL+E + if (!c || !isShortcut) // do not process the shortcut when we have a completer + QTextEdit::keyPressEvent(e); +//! [7] + +//! [8] + const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier); + if (!c || (ctrlOrShift && e->text().isEmpty())) + return; + + static QString eow("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-="); // end of word + bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift; + QString completionPrefix = textUnderCursor(); + + if (!isShortcut && (hasModifier || e->text().isEmpty()|| completionPrefix.length() < 3 + || eow.contains(e->text().right(1)))) { + c->popup()->hide(); + return; + } + + if (completionPrefix != c->completionPrefix()) { + c->setCompletionPrefix(completionPrefix); + c->popup()->setCurrentIndex(c->completionModel()->index(0, 0)); + } + QRect cr = cursorRect(); + cr.setWidth(c->popup()->sizeHintForColumn(0) + + c->popup()->verticalScrollBar()->sizeHint().width()); + c->complete(cr); // popup it up! +} +//! [8] + diff --git a/examples/widgets/tools/customcompleter/textedit.h b/examples/widgets/tools/customcompleter/textedit.h new file mode 100644 index 0000000000..67d83241e3 --- /dev/null +++ b/examples/widgets/tools/customcompleter/textedit.h @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TEXTEDIT_H +#define TEXTEDIT_H + +#include <QTextEdit> + +QT_BEGIN_NAMESPACE +class QCompleter; +QT_END_NAMESPACE + +//! [0] +class TextEdit : public QTextEdit +{ + Q_OBJECT + +public: + TextEdit(QWidget *parent = 0); + ~TextEdit(); + + void setCompleter(QCompleter *c); + QCompleter *completer() const; + +protected: + void keyPressEvent(QKeyEvent *e); + void focusInEvent(QFocusEvent *e); + +private slots: + void insertCompletion(const QString &completion); + +private: + QString textUnderCursor() const; + +private: + QCompleter *c; +}; +//! [0] + +#endif // TEXTEDIT_H + diff --git a/examples/widgets/tools/echoplugin/echoplugin.pro b/examples/widgets/tools/echoplugin/echoplugin.pro new file mode 100644 index 0000000000..ffd48fd25f --- /dev/null +++ b/examples/widgets/tools/echoplugin/echoplugin.pro @@ -0,0 +1,13 @@ +#! [0] +TEMPLATE = subdirs +SUBDIRS = echowindow \ + plugin +#! [0] + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tools/echoplugin +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS echoplugin.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/tools/echoplugin +INSTALLS += target sources + +QT += widgets diff --git a/examples/widgets/tools/echoplugin/echowindow/echointerface.h b/examples/widgets/tools/echoplugin/echowindow/echointerface.h new file mode 100644 index 0000000000..9c85fdccff --- /dev/null +++ b/examples/widgets/tools/echoplugin/echowindow/echointerface.h @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef ECHOINTERFACE_H +#define ECHOINTERFACE_H + +#include <QString> + +//! [0] +class EchoInterface +{ +public: + virtual ~EchoInterface() {} + virtual QString echo(const QString &message) = 0; +}; + + +QT_BEGIN_NAMESPACE + +#define EchoInterface_iid "org.qt-project.Qt.Examples.EchoInterface" + +Q_DECLARE_INTERFACE(EchoInterface, EchoInterface_iid) +QT_END_NAMESPACE + +//! [0] +#endif diff --git a/examples/widgets/tools/echoplugin/echowindow/echowindow.cpp b/examples/widgets/tools/echoplugin/echowindow/echowindow.cpp new file mode 100644 index 0000000000..7cc97e16eb --- /dev/null +++ b/examples/widgets/tools/echoplugin/echowindow/echowindow.cpp @@ -0,0 +1,118 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include "echowindow.h" + +//! [0] +EchoWindow::EchoWindow() +{ + createGUI(); + setLayout(layout); + setWindowTitle("Echo Plugin Example"); + + if (!loadPlugin()) { + QMessageBox::information(this, "Error", "Could not load the plugin"); + lineEdit->setEnabled(false); + button->setEnabled(false); + } +} +//! [0] + +//! [1] +void EchoWindow::sendEcho() +{ + QString text = echoInterface->echo(lineEdit->text()); + label->setText(text); +} +//! [1] + +//! [2] +void EchoWindow::createGUI() +{ + lineEdit = new QLineEdit; + label = new QLabel; + label->setFrameStyle(QFrame::Box | QFrame::Plain); + button = new QPushButton(tr("Send Message")); + + connect(lineEdit, SIGNAL(editingFinished()), + this, SLOT(sendEcho())); + connect(button, SIGNAL(clicked()), + this, SLOT(sendEcho())); + + layout = new QGridLayout; + layout->addWidget(new QLabel(tr("Message:")), 0, 0); + layout->addWidget(lineEdit, 0, 1); + layout->addWidget(new QLabel(tr("Answer:")), 1, 0); + layout->addWidget(label, 1, 1); + layout->addWidget(button, 2, 1, Qt::AlignRight); + layout->setSizeConstraint(QLayout::SetFixedSize); +} +//! [2] + +//! [3] +bool EchoWindow::loadPlugin() +{ + QDir pluginsDir(qApp->applicationDirPath()); +#if defined(Q_OS_WIN) + if (pluginsDir.dirName().toLower() == "debug" || pluginsDir.dirName().toLower() == "release") + pluginsDir.cdUp(); +#elif defined(Q_OS_MAC) + if (pluginsDir.dirName() == "MacOS") { + pluginsDir.cdUp(); + pluginsDir.cdUp(); + pluginsDir.cdUp(); + } +#endif + pluginsDir.cd("plugins"); + foreach (QString fileName, pluginsDir.entryList(QDir::Files)) { + QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName)); + QObject *plugin = pluginLoader.instance(); + if (plugin) { + echoInterface = qobject_cast<EchoInterface *>(plugin); + if (echoInterface) + return true; + } + } + + return false; +} +//! [3] diff --git a/examples/widgets/tools/echoplugin/echowindow/echowindow.desktop b/examples/widgets/tools/echoplugin/echowindow/echowindow.desktop new file mode 100644 index 0000000000..7b36de401d --- /dev/null +++ b/examples/widgets/tools/echoplugin/echowindow/echowindow.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=Echo Window +Exec=/opt/usr/bin/echowindow +Icon=echowindow +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tools/echoplugin/echowindow/echowindow.h b/examples/widgets/tools/echoplugin/echowindow/echowindow.h new file mode 100644 index 0000000000..fca3c707e1 --- /dev/null +++ b/examples/widgets/tools/echoplugin/echowindow/echowindow.h @@ -0,0 +1,79 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef ECHODIALOG_H +#define ECHODIALOG_H + +#include <QWidget> + +#include "echointerface.h" + +QT_BEGIN_NAMESPACE +class QString; +class QLineEdit; +class QLabel; +class QPushButton; +class QGridLayout; +QT_END_NAMESPACE + +//! [0] +class EchoWindow : public QWidget +{ + Q_OBJECT + +public: + EchoWindow(); + +private slots: + void sendEcho(); + +private: + void createGUI(); + bool loadPlugin(); + + EchoInterface *echoInterface; + QLineEdit *lineEdit; + QLabel *label; + QPushButton *button; + QGridLayout *layout; +}; +//! [0] + +#endif diff --git a/examples/widgets/tools/echoplugin/echowindow/echowindow.pro b/examples/widgets/tools/echoplugin/echowindow/echowindow.pro new file mode 100644 index 0000000000..90bc831de1 --- /dev/null +++ b/examples/widgets/tools/echoplugin/echowindow/echowindow.pro @@ -0,0 +1,23 @@ +HEADERS = echowindow.h \ + echointerface.h +SOURCES = echowindow.cpp \ + main.cpp + +TARGET = echoplugin +QMAKE_PROJECT_NAME = echopluginwindow +win32 { + CONFIG(debug, release|debug):DESTDIR = ../debug/ + CONFIG(release, release|debug):DESTDIR = ../release/ +} else { + DESTDIR = ../ +} + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tools/echoplugin +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS echowindow.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/tools/echoplugin/echowindow +INSTALLS += target sources + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tools/echoplugin/echowindow/main.cpp b/examples/widgets/tools/echoplugin/echowindow/main.cpp new file mode 100644 index 0000000000..e49b761170 --- /dev/null +++ b/examples/widgets/tools/echoplugin/echowindow/main.cpp @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include "echowindow.h" +#include "echointerface.h" + +//! [0] +int main(int argv, char *args[]) +{ + QApplication app(argv, args); + + EchoWindow window; + window.show(); + + return app.exec(); +} +//! [0] diff --git a/examples/widgets/tools/echoplugin/plugin/echoplugin.cpp b/examples/widgets/tools/echoplugin/plugin/echoplugin.cpp new file mode 100644 index 0000000000..5345c5352d --- /dev/null +++ b/examples/widgets/tools/echoplugin/plugin/echoplugin.cpp @@ -0,0 +1,50 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include "echoplugin.h" + +//! [0] +QString EchoPlugin::echo(const QString &message) +{ + return message; +} +//! [0] diff --git a/examples/widgets/tools/echoplugin/plugin/echoplugin.h b/examples/widgets/tools/echoplugin/plugin/echoplugin.h new file mode 100644 index 0000000000..69d76f76fd --- /dev/null +++ b/examples/widgets/tools/echoplugin/plugin/echoplugin.h @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef ECHOPLUGIN_H +#define ECHOPLUGIN_H + +#include <QObject> +#include <QtPlugin> +#include "echointerface.h" + +//! [0] +class EchoPlugin : public QObject, EchoInterface +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "org.qt-project.Qt.Examples.EchoInterface" FILE "echoplugin.json") + Q_INTERFACES(EchoInterface) + +public: + QString echo(const QString &message); +}; +//! [0] + +#endif diff --git a/examples/widgets/tools/echoplugin/plugin/echoplugin.json b/examples/widgets/tools/echoplugin/plugin/echoplugin.json new file mode 100644 index 0000000000..0967ef424b --- /dev/null +++ b/examples/widgets/tools/echoplugin/plugin/echoplugin.json @@ -0,0 +1 @@ +{} diff --git a/examples/widgets/tools/echoplugin/plugin/plugin.desktop b/examples/widgets/tools/echoplugin/plugin/plugin.desktop new file mode 100644 index 0000000000..5aba4d1c87 --- /dev/null +++ b/examples/widgets/tools/echoplugin/plugin/plugin.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=Echo Plugin +Exec=/opt/usr/bin/plugin +Icon=plugin +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tools/echoplugin/plugin/plugin.pro b/examples/widgets/tools/echoplugin/plugin/plugin.pro new file mode 100644 index 0000000000..532a382586 --- /dev/null +++ b/examples/widgets/tools/echoplugin/plugin/plugin.pro @@ -0,0 +1,18 @@ +#! [0] +TEMPLATE = lib +CONFIG += plugin +INCLUDEPATH += ../echowindow +HEADERS = echoplugin.h +SOURCES = echoplugin.cpp +OTHER_FILES += echoplugin.json +TARGET = $$qtLibraryTarget(echoplugin) +DESTDIR = ../plugins +#! [0] + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tools/echoplugin/plugin +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS plugin.pro echoplugin.json +sources.path = $$[QT_INSTALL_EXAMPLES]/tools/echoplugin/plugin +INSTALLS += target sources + +QT += widgets diff --git a/examples/widgets/tools/i18n/i18n.desktop b/examples/widgets/tools/i18n/i18n.desktop new file mode 100644 index 0000000000..e1632c4d04 --- /dev/null +++ b/examples/widgets/tools/i18n/i18n.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=I18N +Exec=/opt/usr/bin/i18n +Icon=i18n +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tools/i18n/i18n.pro b/examples/widgets/tools/i18n/i18n.pro new file mode 100644 index 0000000000..a02e25dcd0 --- /dev/null +++ b/examples/widgets/tools/i18n/i18n.pro @@ -0,0 +1,30 @@ +HEADERS = languagechooser.h \ + mainwindow.h +SOURCES = languagechooser.cpp \ + main.cpp \ + mainwindow.cpp +RESOURCES += i18n.qrc +TRANSLATIONS += translations/i18n_ar.ts \ + translations/i18n_cs.ts \ + translations/i18n_de.ts \ + translations/i18n_el.ts \ + translations/i18n_en.ts \ + translations/i18n_eo.ts \ + translations/i18n_fr.ts \ + translations/i18n_it.ts \ + translations/i18n_jp.ts \ + translations/i18n_ko.ts \ + translations/i18n_no.ts \ + translations/i18n_ru.ts \ + translations/i18n_sv.ts \ + translations/i18n_zh.ts + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tools/i18n +sources.files = $$SOURCES $$HEADERS $$RESOURCES translations i18n.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/tools/i18n +INSTALLS += target sources + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tools/i18n/i18n.qrc b/examples/widgets/tools/i18n/i18n.qrc new file mode 100644 index 0000000000..16a89f140b --- /dev/null +++ b/examples/widgets/tools/i18n/i18n.qrc @@ -0,0 +1,18 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>translations/i18n_ar.qm</file> + <file>translations/i18n_cs.qm</file> + <file>translations/i18n_de.qm</file> + <file>translations/i18n_el.qm</file> + <file>translations/i18n_en.qm</file> + <file>translations/i18n_eo.qm</file> + <file>translations/i18n_fr.qm</file> + <file>translations/i18n_it.qm</file> + <file>translations/i18n_jp.qm</file> + <file>translations/i18n_ko.qm</file> + <file>translations/i18n_no.qm</file> + <file>translations/i18n_ru.qm</file> + <file>translations/i18n_sv.qm</file> + <file>translations/i18n_zh.qm</file> +</qresource> +</RCC> diff --git a/examples/widgets/tools/i18n/languagechooser.cpp b/examples/widgets/tools/i18n/languagechooser.cpp new file mode 100644 index 0000000000..0728e29547 --- /dev/null +++ b/examples/widgets/tools/i18n/languagechooser.cpp @@ -0,0 +1,166 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include "languagechooser.h" +#include "mainwindow.h" + +#ifdef Q_WS_MAC +QT_BEGIN_NAMESPACE +extern void qt_mac_set_menubar_merge(bool merge); +QT_END_NAMESPACE +#endif + +LanguageChooser::LanguageChooser(QWidget *parent) + : QDialog(parent, Qt::WindowStaysOnTopHint) +{ + groupBox = new QGroupBox("Languages"); + + QGridLayout *groupBoxLayout = new QGridLayout; + + QStringList qmFiles = findQmFiles(); + for (int i = 0; i < qmFiles.size(); ++i) { + QCheckBox *checkBox = new QCheckBox(languageName(qmFiles[i])); + qmFileForCheckBoxMap.insert(checkBox, qmFiles[i]); + connect(checkBox, SIGNAL(toggled(bool)), this, SLOT(checkBoxToggled())); + groupBoxLayout->addWidget(checkBox, i / 2, i % 2); + } + groupBox->setLayout(groupBoxLayout); + + buttonBox = new QDialogButtonBox; + + showAllButton = buttonBox->addButton("Show All", + QDialogButtonBox::ActionRole); + hideAllButton = buttonBox->addButton("Hide All", + QDialogButtonBox::ActionRole); + + connect(showAllButton, SIGNAL(clicked()), this, SLOT(showAll())); + connect(hideAllButton, SIGNAL(clicked()), this, SLOT(hideAll())); + + QVBoxLayout *mainLayout = new QVBoxLayout; + mainLayout->addWidget(groupBox); + mainLayout->addWidget(buttonBox); + setLayout(mainLayout); + +#ifdef Q_WS_MAC + qt_mac_set_menubar_merge(false); +#endif + + setWindowTitle("I18N"); +} + +bool LanguageChooser::eventFilter(QObject *object, QEvent *event) +{ + if (event->type() == QEvent::Close) { + MainWindow *window = qobject_cast<MainWindow *>(object); + if (window) { + QCheckBox *checkBox = mainWindowForCheckBoxMap.key(window); + if (checkBox) + checkBox->setChecked(false); + } + } + return QWidget::eventFilter(object, event); +} + +void LanguageChooser::closeEvent(QCloseEvent * /* event */) +{ + qApp->quit(); +} + +void LanguageChooser::checkBoxToggled() +{ + QCheckBox *checkBox = qobject_cast<QCheckBox *>(sender()); + MainWindow *window = mainWindowForCheckBoxMap[checkBox]; + if (!window) { + QTranslator translator; + translator.load(qmFileForCheckBoxMap[checkBox]); + qApp->installTranslator(&translator); + + window = new MainWindow; + window->setPalette(colorForLanguage(checkBox->text())); + + window->installEventFilter(this); + mainWindowForCheckBoxMap.insert(checkBox, window); + } + window->setVisible(checkBox->isChecked()); +} + +void LanguageChooser::showAll() +{ + foreach (QCheckBox *checkBox, qmFileForCheckBoxMap.keys()) + checkBox->setChecked(true); +} + +void LanguageChooser::hideAll() +{ + foreach (QCheckBox *checkBox, qmFileForCheckBoxMap.keys()) + checkBox->setChecked(false); +} + +QStringList LanguageChooser::findQmFiles() +{ + QDir dir(":/translations"); + QStringList fileNames = dir.entryList(QStringList("*.qm"), QDir::Files, + QDir::Name); + QMutableStringListIterator i(fileNames); + while (i.hasNext()) { + i.next(); + i.setValue(dir.filePath(i.value())); + } + return fileNames; +} + +QString LanguageChooser::languageName(const QString &qmFile) +{ + QTranslator translator; + translator.load(qmFile); + + return translator.translate("MainWindow", "English"); +} + +QColor LanguageChooser::colorForLanguage(const QString &language) +{ + uint hashValue = qHash(language); + int red = 156 + (hashValue & 0x3F); + int green = 156 + ((hashValue >> 6) & 0x3F); + int blue = 156 + ((hashValue >> 12) & 0x3F); + return QColor(red, green, blue); +} diff --git a/examples/widgets/tools/i18n/languagechooser.h b/examples/widgets/tools/i18n/languagechooser.h new file mode 100644 index 0000000000..4d350674e5 --- /dev/null +++ b/examples/widgets/tools/i18n/languagechooser.h @@ -0,0 +1,85 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef LANGUAGECHOOSER_H +#define LANGUAGECHOOSER_H + +#include <QDialog> +#include <QMap> +#include <QStringList> + +QT_BEGIN_NAMESPACE +class QAbstractButton; +class QCheckBox; +class QDialogButtonBox; +class QGroupBox; +QT_END_NAMESPACE +class MainWindow; + +class LanguageChooser : public QDialog +{ + Q_OBJECT + +public: + LanguageChooser(QWidget *parent = 0); + +protected: + bool eventFilter(QObject *object, QEvent *event); + void closeEvent(QCloseEvent *event); + +private slots: + void checkBoxToggled(); + void showAll(); + void hideAll(); + +private: + QStringList findQmFiles(); + QString languageName(const QString &qmFile); + QColor colorForLanguage(const QString &language); + + QGroupBox *groupBox; + QDialogButtonBox *buttonBox; + QAbstractButton *showAllButton; + QAbstractButton *hideAllButton; + QMap<QCheckBox *, QString> qmFileForCheckBoxMap; + QMap<QCheckBox *, MainWindow *> mainWindowForCheckBoxMap; +}; + +#endif diff --git a/examples/widgets/tools/i18n/main.cpp b/examples/widgets/tools/i18n/main.cpp new file mode 100644 index 0000000000..f57f2b4b2e --- /dev/null +++ b/examples/widgets/tools/i18n/main.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> + +#include "languagechooser.h" +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(i18n); + + QApplication app(argc, argv); + LanguageChooser chooser; + chooser.show(); + return app.exec(); +} diff --git a/examples/widgets/tools/i18n/mainwindow.cpp b/examples/widgets/tools/i18n/mainwindow.cpp new file mode 100644 index 0000000000..a2e6da5e38 --- /dev/null +++ b/examples/widgets/tools/i18n/mainwindow.cpp @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include "mainwindow.h" + +static const char * const listEntries[] = { + QT_TRANSLATE_NOOP("MainWindow", "First"), + QT_TRANSLATE_NOOP("MainWindow", "Second"), + QT_TRANSLATE_NOOP("MainWindow", "Third"), + 0 +}; + +MainWindow::MainWindow() +{ + centralWidget = new QWidget; + setCentralWidget(centralWidget); + + createGroupBox(); + + listWidget = new QListWidget; + for (int i = 0; listEntries[i]; ++i) + listWidget->addItem(tr(listEntries[i])); + + QVBoxLayout *mainLayout = new QVBoxLayout; + mainLayout->addWidget(groupBox); + mainLayout->addWidget(listWidget); + centralWidget->setLayout(mainLayout); + + exitAction = new QAction(tr("E&xit"), this); + connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit())); + + fileMenu = menuBar()->addMenu(tr("&File")); + fileMenu->setPalette(QPalette(Qt::red)); + fileMenu->addAction(exitAction); + + setWindowTitle(tr("Language: %1").arg(tr("English"))); + statusBar()->showMessage(tr("Internationalization Example")); + + if (tr("LTR") == "RTL") + setLayoutDirection(Qt::RightToLeft); +} + +void MainWindow::createGroupBox() +{ + groupBox = new QGroupBox(tr("View")); + perspectiveRadioButton = new QRadioButton(tr("Perspective")); + isometricRadioButton = new QRadioButton(tr("Isometric")); + obliqueRadioButton = new QRadioButton(tr("Oblique")); + perspectiveRadioButton->setChecked(true); + + QVBoxLayout *groupBoxLayout = new QVBoxLayout; + groupBoxLayout->addWidget(perspectiveRadioButton); + groupBoxLayout->addWidget(isometricRadioButton); + groupBoxLayout->addWidget(obliqueRadioButton); + groupBox->setLayout(groupBoxLayout); +} diff --git a/examples/widgets/tools/i18n/mainwindow.h b/examples/widgets/tools/i18n/mainwindow.h new file mode 100644 index 0000000000..ab1131227d --- /dev/null +++ b/examples/widgets/tools/i18n/mainwindow.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> + +QT_BEGIN_NAMESPACE +class QAction; +class QGroupBox; +class QLabel; +class QListWidget; +class QMenu; +class QRadioButton; +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +private: + void createGroupBox(); + + QWidget *centralWidget; + QLabel *label; + QGroupBox *groupBox; + QListWidget *listWidget; + QRadioButton *perspectiveRadioButton; + QRadioButton *isometricRadioButton; + QRadioButton *obliqueRadioButton; + QMenu *fileMenu; + QAction *exitAction; +}; + +#endif diff --git a/examples/widgets/tools/i18n/translations/i18n_ar.qm b/examples/widgets/tools/i18n/translations/i18n_ar.qm Binary files differnew file mode 100644 index 0000000000..a134c468bb --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_ar.qm diff --git a/examples/widgets/tools/i18n/translations/i18n_ar.ts b/examples/widgets/tools/i18n/translations/i18n_ar.ts new file mode 100644 index 0000000000..a7ec2c9404 --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_ar.ts @@ -0,0 +1,57 @@ +<!DOCTYPE TS><TS> +<context> + <name>MainWindow</name> + <message> + <source>First</source> + <translation>أول</translation> + </message> + <message> + <source>Internationalization Example</source> + <translation>مثال التدويل</translation> + </message> + <message> + <source>Isometric</source> + <translation>متماثل</translation> + </message> + <message> + <source>Language: %1</source> + <translation>اللغة: %1</translation> + </message> + <message> + <source>English</source> + <translation>العربية</translation> + </message> + <message> + <source>Oblique</source> + <translation>مصمت</translation> + </message> + <message> + <source>Perspective</source> + <translation>منظور</translation> + </message> + <message> + <source>Second</source> + <translation>ثانى</translation> + </message> + <message> + <source>Third</source> + <translation>ثالث</translation> + </message> + <message> + <source>View</source> + <translation>مرئى</translation> + </message> + <message> + <source>E&xit</source> + <translation>أخرج</translation> + </message> + <message> + <source>&File</source> + <translation>الملÙ</translation> + </message> + <message> + <source>LTR</source> + <translation>RTL</translation> + </message> +</context> +</TS> diff --git a/examples/widgets/tools/i18n/translations/i18n_cs.qm b/examples/widgets/tools/i18n/translations/i18n_cs.qm Binary files differnew file mode 100644 index 0000000000..5b7ff95b05 --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_cs.qm diff --git a/examples/widgets/tools/i18n/translations/i18n_cs.ts b/examples/widgets/tools/i18n/translations/i18n_cs.ts new file mode 100644 index 0000000000..6c4dee9b2e --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_cs.ts @@ -0,0 +1,57 @@ +<!DOCTYPE TS><TS> +<context> + <name>MainWindow</name> + <message> + <source>View</source> + <translation>Pohled</translation> + </message> + <message> + <source>&File</source> + <translation>&Soubor</translation> + </message> + <message> + <source>E&xit</source> + <translation>&Konec</translation> + </message> + <message> + <source>First</source> + <translation>PrvnÃ</translation> + </message> + <message> + <source>Third</source> + <translation>TÅ™etÃ</translation> + </message> + <message> + <source>Language: %1</source> + <translation>Jayzk: %1</translation> + </message> + <message> + <source>English</source> + <translation>ÄŒeský</translation> + </message> + <message> + <source>Oblique</source> + <translation>NaklonÄ›ný</translation> + </message> + <message> + <source>Second</source> + <translation>Druhý</translation> + </message> + <message> + <source>Isometric</source> + <translation>Isometrický</translation> + </message> + <message> + <source>Perspective</source> + <translation>PerspektivnÃ</translation> + </message> + <message> + <source>Internationalization Example</source> + <translation>Ukázka lokalizace</translation> + </message> + <message> + <source>LTR</source> + <translation>LTR</translation> + </message> +</context> +</TS> diff --git a/examples/widgets/tools/i18n/translations/i18n_de.qm b/examples/widgets/tools/i18n/translations/i18n_de.qm Binary files differnew file mode 100644 index 0000000000..177fc49b8b --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_de.qm diff --git a/examples/widgets/tools/i18n/translations/i18n_de.ts b/examples/widgets/tools/i18n/translations/i18n_de.ts new file mode 100644 index 0000000000..249a61d020 --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_de.ts @@ -0,0 +1,57 @@ +<!DOCTYPE TS><TS> +<context> + <name>MainWindow</name> + <message> + <source>View</source> + <translation>Ansicht</translation> + </message> + <message> + <source>&File</source> + <translation>&Datei</translation> + </message> + <message> + <source>E&xit</source> + <translation>Be&enden</translation> + </message> + <message> + <source>First</source> + <translation>Erstens</translation> + </message> + <message> + <source>Third</source> + <translation>Drittens</translation> + </message> + <message> + <source>English</source> + <translation>Deutsch</translation> + </message> + <message> + <source>Language: %1</source> + <translation>Sprache: %1</translation> + </message> + <message> + <source>Oblique</source> + <translation>Schief</translation> + </message> + <message> + <source>Second</source> + <translation>Zweitens</translation> + </message> + <message> + <source>Isometric</source> + <translation>Isometrisch</translation> + </message> + <message> + <source>Perspective</source> + <translation>Perspektivisch</translation> + </message> + <message> + <source>Internationalization Example</source> + <translation>Internationalisierungsbeispiel</translation> + </message> + <message> + <source>LTR</source> + <translation>LTR</translation> + </message> +</context> +</TS> diff --git a/examples/widgets/tools/i18n/translations/i18n_el.qm b/examples/widgets/tools/i18n/translations/i18n_el.qm Binary files differnew file mode 100644 index 0000000000..5483291bb0 --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_el.qm diff --git a/examples/widgets/tools/i18n/translations/i18n_el.ts b/examples/widgets/tools/i18n/translations/i18n_el.ts new file mode 100644 index 0000000000..d23a0aad3e --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_el.ts @@ -0,0 +1,57 @@ +<!DOCTYPE TS><TS> +<context> + <name>MainWindow</name> + <message> + <source>&File</source> + <translation>&ΑÏχείο</translation> + </message> + <message> + <source>E&xit</source> + <translation>Έ&ξοδος</translation> + </message> + <message> + <source>First</source> + <translation>Î Ïώτο</translation> + </message> + <message> + <source>Internationalization Example</source> + <translation>ΠαÏάδειγμα διεθνοποίησης</translation> + </message> + <message> + <source>Isometric</source> + <translation>ΙσομετÏική</translation> + </message> + <message> + <source>Language: %1</source> + <translation>Γλώσσα: %1</translation> + </message> + <message> + <source>English</source> + <translation>Ελληνικά</translation> + </message> + <message> + <source>Oblique</source> + <translation>Πλάγια</translation> + </message> + <message> + <source>Perspective</source> + <translation>Î Ïοοπτική</translation> + </message> + <message> + <source>Second</source> + <translation>ΔεÏτεÏο</translation> + </message> + <message> + <source>Third</source> + <translation>ΤÏίτο</translation> + </message> + <message> + <source>View</source> + <translation>Όψη</translation> + </message> + <message> + <source>LTR</source> + <translation>LTR</translation> + </message> +</context> +</TS> diff --git a/examples/widgets/tools/i18n/translations/i18n_en.qm b/examples/widgets/tools/i18n/translations/i18n_en.qm Binary files differnew file mode 100644 index 0000000000..9190ac7e6f --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_en.qm diff --git a/examples/widgets/tools/i18n/translations/i18n_en.ts b/examples/widgets/tools/i18n/translations/i18n_en.ts new file mode 100644 index 0000000000..ca38e958c1 --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_en.ts @@ -0,0 +1,57 @@ +<!DOCTYPE TS><TS> +<context> + <name>MainWindow</name> + <message> + <source>E&xit</source> + <translation>E&xit</translation> + </message> + <message> + <source>&File</source> + <translation>&File</translation> + </message> + <message> + <source>Internationalization Example</source> + <translation>Internationalization Example</translation> + </message> + <message> + <source>Language: %1</source> + <translation>Language: %1</translation> + </message> + <message> + <source>English</source> + <translation>English</translation> + </message> + <message> + <source>View</source> + <translation>View</translation> + </message> + <message> + <source>Perspective</source> + <translation>Perspective</translation> + </message> + <message> + <source>Isometric</source> + <translation>Isometric</translation> + </message> + <message> + <source>Oblique</source> + <translation>Oblique</translation> + </message> + <message> + <source>First</source> + <translation>First</translation> + </message> + <message> + <source>Second</source> + <translation>Second</translation> + </message> + <message> + <source>Third</source> + <translation>Third</translation> + </message> + <message> + <source>LTR</source> + <translation>LTR</translation> + </message> +</context> +</TS> diff --git a/examples/widgets/tools/i18n/translations/i18n_eo.qm b/examples/widgets/tools/i18n/translations/i18n_eo.qm Binary files differnew file mode 100644 index 0000000000..a8457bef06 --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_eo.qm diff --git a/examples/widgets/tools/i18n/translations/i18n_eo.ts b/examples/widgets/tools/i18n/translations/i18n_eo.ts new file mode 100644 index 0000000000..16a37becd9 --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_eo.ts @@ -0,0 +1,57 @@ +<!DOCTYPE TS><TS> +<context> + <name>MainWindow</name> + <message> + <source>&File</source> + <translation>&Dosiero</translation> + </message> + <message> + <source>First</source> + <translation>Unue</translation> + </message> + <message> + <source>Internationalization Example</source> + <translation>Ekzemplo pri internaciigo</translation> + </message> + <message> + <source>Isometric</source> + <translation>Isometria</translation> + </message> + <message> + <source>Language: %1</source> + <translation>Lingvo: %1</translation> + </message> + <message> + <source>English</source> + <translation>Esperanto</translation> + </message> + <message> + <source>Oblique</source> + <translation>Oblikva</translation> + </message> + <message> + <source>Perspective</source> + <translation>Perspektiva</translation> + </message> + <message> + <source>Second</source> + <translation>Due</translation> + </message> + <message> + <source>Third</source> + <translation>Trie</translation> + </message> + <message> + <source>View</source> + <translation>Aspekto</translation> + </message> + <message> + <source>E&xit</source> + <translation>&Fini</translation> + </message> + <message> + <source>LTR</source> + <translation>LTR</translation> + </message> +</context> +</TS> diff --git a/examples/widgets/tools/i18n/translations/i18n_fr.qm b/examples/widgets/tools/i18n/translations/i18n_fr.qm Binary files differnew file mode 100644 index 0000000000..3e8a69b80d --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_fr.qm diff --git a/examples/widgets/tools/i18n/translations/i18n_fr.ts b/examples/widgets/tools/i18n/translations/i18n_fr.ts new file mode 100644 index 0000000000..0012892ef2 --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_fr.ts @@ -0,0 +1,57 @@ +<!DOCTYPE TS><TS> +<context> + <name>MainWindow</name> + <message> + <source>View</source> + <translation>Vue</translation> + </message> + <message> + <source>&File</source> + <translation>&Fichier</translation> + </message> + <message> + <source>E&xit</source> + <translation>&Quitter</translation> + </message> + <message> + <source>First</source> + <translation>Premier</translation> + </message> + <message> + <source>Third</source> + <translation>Troisième</translation> + </message> + <message> + <source>Language: %1</source> + <translation>Langue : %1</translation> + </message> + <message> + <source>English</source> + <translation>Français</translation> + </message> + <message> + <source>Oblique</source> + <translation>Oblique</translation> + </message> + <message> + <source>Second</source> + <translation>Deuxième</translation> + </message> + <message> + <source>Isometric</source> + <translation>Isométrique</translation> + </message> + <message> + <source>Perspective</source> + <translation>Perspective</translation> + </message> + <message> + <source>Internationalization Example</source> + <translation>Exemple d'internationalisation</translation> + </message> + <message> + <source>LTR</source> + <translation>LTR</translation> + </message> +</context> +</TS> diff --git a/examples/widgets/tools/i18n/translations/i18n_it.qm b/examples/widgets/tools/i18n/translations/i18n_it.qm Binary files differnew file mode 100644 index 0000000000..3dffd30548 --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_it.qm diff --git a/examples/widgets/tools/i18n/translations/i18n_it.ts b/examples/widgets/tools/i18n/translations/i18n_it.ts new file mode 100644 index 0000000000..d516a277b6 --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_it.ts @@ -0,0 +1,57 @@ +<!DOCTYPE TS><TS> +<context> + <name>MainWindow</name> + <message> + <source>First</source> + <translation>Primo</translation> + </message> + <message> + <source>Internationalization Example</source> + <translation>Esempio di localizzazione</translation> + </message> + <message> + <source>Isometric</source> + <translation>Isometrica</translation> + </message> + <message> + <source>Language: %1</source> + <translation>Lingua: %1</translation> + </message> + <message> + <source>English</source> + <translation>Italiano</translation> + </message> + <message> + <source>Oblique</source> + <translation>Obliqua</translation> + </message> + <message> + <source>Perspective</source> + <translation>Prospettica</translation> + </message> + <message> + <source>Second</source> + <translation>Secondo</translation> + </message> + <message> + <source>Third</source> + <translation>Terzo</translation> + </message> + <message> + <source>View</source> + <translation>Vista</translation> + </message> + <message> + <source>E&xit</source> + <translation>&Esci</translation> + </message> + <message> + <source>&File</source> + <translation>&File</translation> + </message> + <message> + <source>LTR</source> + <translation>LTR</translation> + </message> +</context> +</TS> diff --git a/examples/widgets/tools/i18n/translations/i18n_jp.qm b/examples/widgets/tools/i18n/translations/i18n_jp.qm Binary files differnew file mode 100644 index 0000000000..017bc96cfc --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_jp.qm diff --git a/examples/widgets/tools/i18n/translations/i18n_jp.ts b/examples/widgets/tools/i18n/translations/i18n_jp.ts new file mode 100644 index 0000000000..067b5a8a0b --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_jp.ts @@ -0,0 +1,57 @@ +<!DOCTYPE TS><TS> +<context> + <name>MainWindow</name> + <message> + <source>&File</source> + <translation>ファイル(&F)</translation> + </message> + <message> + <source>E&xit</source> + <translation>終了(&X)</translation> + </message> + <message> + <source>First</source> + <translation>第一行</translation> + </message> + <message> + <source>Internationalization Example</source> + <translation>国際化(i18n)ã®ä¾‹</translation> + </message> + <message> + <source>Isometric</source> + <translation>ç‰è§’投影法</translation> + </message> + <message> + <source>Language: %1</source> + <translation>言語: %1</translation> + </message> + <message> + <source>English</source> + <translation>日本語</translation> + </message> + <message> + <source>Oblique</source> + <translation>æ–œã‚æŠ•å½±æ³•</translation> + </message> + <message> + <source>Perspective</source> + <translation>é 近法</translation> + </message> + <message> + <source>Second</source> + <translation>第二行</translation> + </message> + <message> + <source>Third</source> + <translation>第三行</translation> + </message> + <message> + <source>View</source> + <translation>表示方å¼</translation> + </message> + <message> + <source>LTR</source> + <translation>LTR</translation> + </message> +</context> +</TS> diff --git a/examples/widgets/tools/i18n/translations/i18n_ko.qm b/examples/widgets/tools/i18n/translations/i18n_ko.qm Binary files differnew file mode 100644 index 0000000000..d61b93db39 --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_ko.qm diff --git a/examples/widgets/tools/i18n/translations/i18n_ko.ts b/examples/widgets/tools/i18n/translations/i18n_ko.ts new file mode 100644 index 0000000000..bfd5924518 --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_ko.ts @@ -0,0 +1,57 @@ +<!DOCTYPE TS><TS> +<context> + <name>MainWindow</name> + <message> + <source>&File</source> + <translation>파ì¼&F</translation> + </message> + <message> + <source>E&xit</source> + <translation>종료&X</translation> + </message> + <message> + <source>First</source> + <translation>첫번째</translation> + </message> + <message> + <source>Internationalization Example</source> + <translation>êµì œí™” ì˜ˆì œ</translation> + </message> + <message> + <source>Isometric</source> + <translation>등측ë„</translation> + </message> + <message> + <source>Language: %1</source> + <translation>언어 : %1</translation> + </message> + <message> + <source>English</source> + <translation>한êµì–´</translation> + </message> + <message> + <source>Oblique</source> + <translation>ë¹—ê°</translation> + </message> + <message> + <source>Perspective</source> + <translation>ì›ê·¼í™”법</translation> + </message> + <message> + <source>Second</source> + <translation>ë‘번째</translation> + </message> + <message> + <source>Third</source> + <translation>세번째</translation> + </message> + <message> + <source>View</source> + <translation>보기</translation> + </message> + <message> + <source>LTR</source> + <translation>LTR</translation> + </message> +</context> +</TS> diff --git a/examples/widgets/tools/i18n/translations/i18n_no.qm b/examples/widgets/tools/i18n/translations/i18n_no.qm Binary files differnew file mode 100644 index 0000000000..c84b0d74ba --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_no.qm diff --git a/examples/widgets/tools/i18n/translations/i18n_no.ts b/examples/widgets/tools/i18n/translations/i18n_no.ts new file mode 100644 index 0000000000..2e06974997 --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_no.ts @@ -0,0 +1,57 @@ +<!DOCTYPE TS><TS> +<context> + <name>MainWindow</name> + <message> + <source>View</source> + <translation>Vis</translation> + </message> + <message> + <source>&File</source> + <translation>&Fil</translation> + </message> + <message> + <source>E&xit</source> + <translation>&Avslutt</translation> + </message> + <message> + <source>First</source> + <translation>Første</translation> + </message> + <message> + <source>Third</source> + <translation>Tredje</translation> + </message> + <message> + <source>Language: %1</source> + <translation>SprÃ¥k: %1</translation> + </message> + <message> + <source>English</source> + <translation>Norsk</translation> + </message> + <message> + <source>Oblique</source> + <translation>Skjevt</translation> + </message> + <message> + <source>Second</source> + <translation>Andre</translation> + </message> + <message> + <source>Isometric</source> + <translation>Isometrisk</translation> + </message> + <message> + <source>Perspective</source> + <translation>Perspektiv</translation> + </message> + <message> + <source>Internationalization Example</source> + <translation>Internasjonaliseringseksempel</translation> + </message> + <message> + <source>LTR</source> + <translation>LTR</translation> + </message> +</context> +</TS> diff --git a/examples/widgets/tools/i18n/translations/i18n_ru.qm b/examples/widgets/tools/i18n/translations/i18n_ru.qm Binary files differnew file mode 100644 index 0000000000..a76e1b8703 --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_ru.qm diff --git a/examples/widgets/tools/i18n/translations/i18n_ru.ts b/examples/widgets/tools/i18n/translations/i18n_ru.ts new file mode 100644 index 0000000000..748cc12fd9 --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_ru.ts @@ -0,0 +1,59 @@ +<?xml version="1.0" encoding="utf-8"?> +<!DOCTYPE TS><TS version="1.1" language="ru"> +<defaultcodec></defaultcodec> +<context> + <name>MainWindow</name> + <message> + <source>View</source> + <translation>Вид</translation> + </message> + <message> + <source>&File</source> + <translation>Файл</translation> + </message> + <message> + <source>E&xit</source> + <translation>Выход</translation> + </message> + <message> + <source>First</source> + <translation>Первый</translation> + </message> + <message> + <source>Third</source> + <translation>Третий</translation> + </message> + <message> + <source>Language: %1</source> + <translation>Язык: %1</translation> + </message> + <message> + <source>English</source> + <translation>РуÑÑкий</translation> + </message> + <message> + <source>Oblique</source> + <translation>КурÑив</translation> + </message> + <message> + <source>Second</source> + <translation>Второй</translation> + </message> + <message> + <source>Isometric</source> + <translation>ИзометричеÑкий</translation> + </message> + <message> + <source>Perspective</source> + <translation>ПерÑпектива</translation> + </message> + <message> + <source>Internationalization Example</source> + <translation>Пример интернационализации</translation> + </message> + <message> + <source>LTR</source> + <translation>LTR</translation> + </message> +</context> +</TS> diff --git a/examples/widgets/tools/i18n/translations/i18n_sv.qm b/examples/widgets/tools/i18n/translations/i18n_sv.qm Binary files differnew file mode 100644 index 0000000000..7204b308b1 --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_sv.qm diff --git a/examples/widgets/tools/i18n/translations/i18n_sv.ts b/examples/widgets/tools/i18n/translations/i18n_sv.ts new file mode 100644 index 0000000000..ac4ab98bd3 --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_sv.ts @@ -0,0 +1,57 @@ +<!DOCTYPE TS><TS> +<context> + <name>MainWindow</name> + <message> + <source>View</source> + <translation>Visa</translation> + </message> + <message> + <source>&File</source> + <translation>&Arkiv</translation> + </message> + <message> + <source>E&xit</source> + <translation>&Avsluta</translation> + </message> + <message> + <source>First</source> + <translation>Första</translation> + </message> + <message> + <source>Third</source> + <translation>Tredje</translation> + </message> + <message> + <source>Language: %1</source> + <translation>SprÃ¥k: %1</translation> + </message> + <message> + <source>English</source> + <translation>Svenska</translation> + </message> + <message> + <source>Oblique</source> + <translation>Skevt</translation> + </message> + <message> + <source>Second</source> + <translation>Andra</translation> + </message> + <message> + <source>Isometric</source> + <translation>Isometriskt</translation> + </message> + <message> + <source>Perspective</source> + <translation>Perspektivt</translation> + </message> + <message> + <source>Internationalization Example</source> + <translation>Internationaliseringsexempel</translation> + </message> + <message> + <source>LTR</source> + <translation>LTR</translation> + </message> +</context> +</TS> diff --git a/examples/widgets/tools/i18n/translations/i18n_zh.qm b/examples/widgets/tools/i18n/translations/i18n_zh.qm Binary files differnew file mode 100644 index 0000000000..32053f4633 --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_zh.qm diff --git a/examples/widgets/tools/i18n/translations/i18n_zh.ts b/examples/widgets/tools/i18n/translations/i18n_zh.ts new file mode 100644 index 0000000000..3b715470a8 --- /dev/null +++ b/examples/widgets/tools/i18n/translations/i18n_zh.ts @@ -0,0 +1,57 @@ +<!DOCTYPE TS><TS> +<context> + <name>MainWindow</name> + <message> + <source>View</source> + <translation>视图</translation> + </message> + <message> + <source>&File</source> + <translation>文件[&F]</translation> + </message> + <message> + <source>E&xit</source> + <translation>退出[&x]</translation> + </message> + <message> + <source>First</source> + <translation>第一个</translation> + </message> + <message> + <source>Third</source> + <translation>第三个</translation> + </message> + <message> + <source>Language: %1</source> + <translation>è¯è¨€: %1</translation> + </message> + <message> + <source>English</source> + <translation>ç®€ä½“ä¸æ–‡</translation> + </message> + <message> + <source>Oblique</source> + <translation>斜投影</translation> + </message> + <message> + <source>Second</source> + <translation>第二个</translation> + </message> + <message> + <source>Isometric</source> + <translation>ç‰è§’投影</translation> + </message> + <message> + <source>Perspective</source> + <translation>é€è§†æŠ•å½±</translation> + </message> + <message> + <source>Internationalization Example</source> + <translation>国际化范例</translation> + </message> + <message> + <source>LTR</source> + <translation>LTR</translation> + </message> +</context> +</TS> diff --git a/examples/widgets/tools/plugandpaint/interfaces.h b/examples/widgets/tools/plugandpaint/interfaces.h new file mode 100644 index 0000000000..b2261a1eb2 --- /dev/null +++ b/examples/widgets/tools/plugandpaint/interfaces.h @@ -0,0 +1,114 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef INTERFACES_H +#define INTERFACES_H + +#include <QtPlugin> + +QT_BEGIN_NAMESPACE +class QImage; +class QPainter; +class QWidget; +class QPainterPath; +class QPoint; +class QRect; +class QString; +class QStringList; +QT_END_NAMESPACE + +//! [0] +class BrushInterface +{ +public: + virtual ~BrushInterface() {} + + virtual QStringList brushes() const = 0; + virtual QRect mousePress(const QString &brush, QPainter &painter, + const QPoint &pos) = 0; + virtual QRect mouseMove(const QString &brush, QPainter &painter, + const QPoint &oldPos, const QPoint &newPos) = 0; + virtual QRect mouseRelease(const QString &brush, QPainter &painter, + const QPoint &pos) = 0; +}; +//! [0] + +//! [1] +class ShapeInterface +{ +public: + virtual ~ShapeInterface() {} + + virtual QStringList shapes() const = 0; + virtual QPainterPath generateShape(const QString &shape, + QWidget *parent) = 0; +}; +//! [1] + +//! [2] +class FilterInterface +{ +public: + virtual ~FilterInterface() {} + + virtual QStringList filters() const = 0; + virtual QImage filterImage(const QString &filter, const QImage &image, + QWidget *parent) = 0; +}; +//! [2] + +QT_BEGIN_NAMESPACE +//! [3] //! [4] +#define BrushInterface_iid "org.qt-project.Qt.Examples.PlugAndPaint.BrushInterface" + +Q_DECLARE_INTERFACE(BrushInterface, BrushInterface_iid) +//! [3] + +#define ShapeInterface_iid "org.qt-project.Qt.Examples.PlugAndPaint.ShapeInterface" + +Q_DECLARE_INTERFACE(ShapeInterface, ShapeInterface_iid) +//! [5] +#define FilterInterface_iid "org.qt-project.Qt.Examples.PlugAndPaint.FilterInterface" + +Q_DECLARE_INTERFACE(FilterInterface, FilterInterface_iid) +//! [4] //! [5] +QT_END_NAMESPACE + +#endif diff --git a/examples/widgets/tools/plugandpaint/main.cpp b/examples/widgets/tools/plugandpaint/main.cpp new file mode 100644 index 0000000000..3b290da62b --- /dev/null +++ b/examples/widgets/tools/plugandpaint/main.cpp @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +#include "mainwindow.h" +#include <QtPlugin> +#include <QApplication> + +Q_IMPORT_PLUGIN(BasicToolsPlugin) + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + MainWindow window; + window.show(); + return app.exec(); +} +//! [0] diff --git a/examples/widgets/tools/plugandpaint/mainwindow.cpp b/examples/widgets/tools/plugandpaint/mainwindow.cpp new file mode 100644 index 0000000000..5626cdb82c --- /dev/null +++ b/examples/widgets/tools/plugandpaint/mainwindow.cpp @@ -0,0 +1,309 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include "interfaces.h" +#include "mainwindow.h" +#include "paintarea.h" +#include "plugindialog.h" + +#include <QPluginLoader> +#include <QTimer> + +#include <QScrollArea> +#include <QMessageBox> +#include <QActionGroup> +#include <QAction> +#include <QMenu> +#include <QMenuBar> +#include <QFileDialog> +#include <QColorDialog> +#include <QInputDialog> +#include <QApplication> + +MainWindow::MainWindow() : + paintArea(new PaintArea), + scrollArea(new QScrollArea) +{ + scrollArea->setBackgroundRole(QPalette::Dark); + scrollArea->setWidget(paintArea); + setCentralWidget(scrollArea); + + createActions(); + createMenus(); + loadPlugins(); + + setWindowTitle(tr("Plug & Paint")); + + if (!brushActionGroup->actions().isEmpty()) + brushActionGroup->actions().first()->trigger(); + + QTimer::singleShot(500, this, SLOT(aboutPlugins())); +} + +void MainWindow::open() +{ + const QString fileName = QFileDialog::getOpenFileName(this, + tr("Open File"), + QDir::currentPath()); + if (!fileName.isEmpty()) { + if (!paintArea->openImage(fileName)) { + QMessageBox::information(this, tr("Plug & Paint"), + tr("Cannot load %1.").arg(fileName)); + return; + } + paintArea->adjustSize(); + } +} + +bool MainWindow::saveAs() +{ + const QString initialPath = QDir::currentPath() + "/untitled.png"; + + const QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), + initialPath); + if (fileName.isEmpty()) { + return false; + } else { + return paintArea->saveImage(fileName, "png"); + } +} + +void MainWindow::brushColor() +{ + const QColor newColor = QColorDialog::getColor(paintArea->brushColor()); + if (newColor.isValid()) + paintArea->setBrushColor(newColor); +} + +void MainWindow::brushWidth() +{ + bool ok; + const int newWidth = QInputDialog::getInt(this, tr("Plug & Paint"), + tr("Select brush width:"), + paintArea->brushWidth(), + 1, 50, 1, &ok); + if (ok) + paintArea->setBrushWidth(newWidth); +} + +//! [0] +void MainWindow::changeBrush() +{ + QAction *action = qobject_cast<QAction *>(sender()); + BrushInterface *iBrush = qobject_cast<BrushInterface *>(action->parent()); + const QString brush = action->text(); + + paintArea->setBrush(iBrush, brush); +} +//! [0] + +//! [1] +void MainWindow::insertShape() +{ + QAction *action = qobject_cast<QAction *>(sender()); + ShapeInterface *iShape = qobject_cast<ShapeInterface *>(action->parent()); + + const QPainterPath path = iShape->generateShape(action->text(), this); + if (!path.isEmpty()) + paintArea->insertShape(path); +} +//! [1] + +//! [2] +void MainWindow::applyFilter() +{ + QAction *action = qobject_cast<QAction *>(sender()); + FilterInterface *iFilter = + qobject_cast<FilterInterface *>(action->parent()); + + const QImage image = iFilter->filterImage(action->text(), paintArea->image(), + this); + paintArea->setImage(image); +} +//! [2] + +void MainWindow::about() +{ + QMessageBox::about(this, tr("About Plug & Paint"), + tr("The <b>Plug & Paint</b> example demonstrates how to write Qt " + "applications that can be extended through plugins.")); +} + +//! [3] +void MainWindow::aboutPlugins() +{ + PluginDialog dialog(pluginsDir.path(), pluginFileNames, this); + dialog.exec(); +} +//! [3] + +void MainWindow::createActions() +{ + openAct = new QAction(tr("&Open..."), this); + openAct->setShortcuts(QKeySequence::Open); + connect(openAct, SIGNAL(triggered()), this, SLOT(open())); + + saveAsAct = new QAction(tr("&Save As..."), this); + saveAsAct->setShortcuts(QKeySequence::SaveAs); + connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs())); + + exitAct = new QAction(tr("E&xit"), this); + exitAct->setShortcuts(QKeySequence::Quit); + connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); + + brushColorAct = new QAction(tr("&Brush Color..."), this); + connect(brushColorAct, SIGNAL(triggered()), this, SLOT(brushColor())); + + brushWidthAct = new QAction(tr("&Brush Width..."), this); + connect(brushWidthAct, SIGNAL(triggered()), this, SLOT(brushWidth())); + + brushActionGroup = new QActionGroup(this); + + aboutAct = new QAction(tr("&About"), this); + connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); + + aboutQtAct = new QAction(tr("About &Qt"), this); + connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); + + aboutPluginsAct = new QAction(tr("About &Plugins"), this); + connect(aboutPluginsAct, SIGNAL(triggered()), this, SLOT(aboutPlugins())); +} + +void MainWindow::createMenus() +{ + fileMenu = menuBar()->addMenu(tr("&File")); + fileMenu->addAction(openAct); + fileMenu->addAction(saveAsAct); + fileMenu->addSeparator(); + fileMenu->addAction(exitAct); + + brushMenu = menuBar()->addMenu(tr("&Brush")); + brushMenu->addAction(brushColorAct); + brushMenu->addAction(brushWidthAct); + brushMenu->addSeparator(); + + shapesMenu = menuBar()->addMenu(tr("&Shapes")); + + filterMenu = menuBar()->addMenu(tr("&Filter")); + + menuBar()->addSeparator(); + + helpMenu = menuBar()->addMenu(tr("&Help")); + helpMenu->addAction(aboutAct); + helpMenu->addAction(aboutQtAct); + helpMenu->addAction(aboutPluginsAct); +} + +//! [4] +void MainWindow::loadPlugins() +{ + foreach (QObject *plugin, QPluginLoader::staticInstances()) + populateMenus(plugin); +//! [4] //! [5] + + pluginsDir = QDir(qApp->applicationDirPath()); + +#if defined(Q_OS_WIN) + if (pluginsDir.dirName().toLower() == "debug" || pluginsDir.dirName().toLower() == "release") + pluginsDir.cdUp(); +#elif defined(Q_OS_MAC) + if (pluginsDir.dirName() == "MacOS") { + pluginsDir.cdUp(); + pluginsDir.cdUp(); + pluginsDir.cdUp(); + } +#endif + pluginsDir.cd("plugins"); +//! [5] + +//! [6] + foreach (QString fileName, pluginsDir.entryList(QDir::Files)) { + QPluginLoader loader(pluginsDir.absoluteFilePath(fileName)); + QObject *plugin = loader.instance(); + if (plugin) { + populateMenus(plugin); + pluginFileNames += fileName; +//! [6] //! [7] + } +//! [7] //! [8] + } +//! [8] + +//! [9] + brushMenu->setEnabled(!brushActionGroup->actions().isEmpty()); + shapesMenu->setEnabled(!shapesMenu->actions().isEmpty()); + filterMenu->setEnabled(!filterMenu->actions().isEmpty()); +} +//! [9] + +//! [10] +void MainWindow::populateMenus(QObject *plugin) +{ + BrushInterface *iBrush = qobject_cast<BrushInterface *>(plugin); + if (iBrush) + addToMenu(plugin, iBrush->brushes(), brushMenu, SLOT(changeBrush()), + brushActionGroup); + + ShapeInterface *iShape = qobject_cast<ShapeInterface *>(plugin); + if (iShape) + addToMenu(plugin, iShape->shapes(), shapesMenu, SLOT(insertShape())); + + FilterInterface *iFilter = qobject_cast<FilterInterface *>(plugin); + if (iFilter) + addToMenu(plugin, iFilter->filters(), filterMenu, SLOT(applyFilter())); +} +//! [10] + +void MainWindow::addToMenu(QObject *plugin, const QStringList &texts, + QMenu *menu, const char *member, + QActionGroup *actionGroup) +{ + foreach (QString text, texts) { + QAction *action = new QAction(text, plugin); + connect(action, SIGNAL(triggered()), this, member); + menu->addAction(action); + + if (actionGroup) { + action->setCheckable(true); + actionGroup->addAction(action); + } + } +} diff --git a/examples/widgets/tools/plugandpaint/mainwindow.h b/examples/widgets/tools/plugandpaint/mainwindow.h new file mode 100644 index 0000000000..c4ea76dec4 --- /dev/null +++ b/examples/widgets/tools/plugandpaint/mainwindow.h @@ -0,0 +1,103 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QDir> +#include <QMainWindow> +#include <QStringList> + +QT_BEGIN_NAMESPACE +class QAction; +class QActionGroup; +class QMenu; +class QScrollArea; +QT_END_NAMESPACE +class PaintArea; + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +private slots: + void open(); + bool saveAs(); + void brushColor(); + void brushWidth(); + void changeBrush(); + void insertShape(); + void applyFilter(); + void about(); + void aboutPlugins(); + +private: + void createActions(); + void createMenus(); + void loadPlugins(); + void populateMenus(QObject *plugin); + void addToMenu(QObject *plugin, const QStringList &texts, QMenu *menu, + const char *member, QActionGroup *actionGroup = 0); + + PaintArea *paintArea; + QScrollArea *scrollArea; + QDir pluginsDir; + QStringList pluginFileNames; + + QMenu *fileMenu; + QMenu *brushMenu; + QMenu *shapesMenu; + QMenu *filterMenu; + QMenu *helpMenu; + QActionGroup *brushActionGroup; + QAction *openAct; + QAction *saveAsAct; + QAction *exitAct; + QAction *brushWidthAct; + QAction *brushColorAct; + QAction *aboutAct; + QAction *aboutQtAct; + QAction *aboutPluginsAct; +}; + +#endif diff --git a/examples/widgets/tools/plugandpaint/paintarea.cpp b/examples/widgets/tools/plugandpaint/paintarea.cpp new file mode 100644 index 0000000000..5cae92cb31 --- /dev/null +++ b/examples/widgets/tools/plugandpaint/paintarea.cpp @@ -0,0 +1,195 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include "interfaces.h" +#include "paintarea.h" + +#include <QPainter> +#include <QMouseEvent> + +PaintArea::PaintArea(QWidget *parent) : + QWidget(parent), + theImage(500, 400, QImage::Format_RGB32), + color(Qt::blue), + thickness(3), + brushInterface(0), + lastPos(-1, -1) +{ + setAttribute(Qt::WA_StaticContents); + setAttribute(Qt::WA_NoBackground); + + theImage.fill(qRgb(255, 255, 255)); +} + +bool PaintArea::openImage(const QString &fileName) +{ + QImage image; + if (!image.load(fileName)) + return false; + + setImage(image); + return true; +} + +bool PaintArea::saveImage(const QString &fileName, const char *fileFormat) +{ + return theImage.save(fileName, fileFormat); +} + +void PaintArea::setImage(const QImage &image) +{ + theImage = image.convertToFormat(QImage::Format_RGB32); + update(); + updateGeometry(); +} + +void PaintArea::insertShape(const QPainterPath &path) +{ + pendingPath = path; +#ifndef QT_NO_CURSOR + setCursor(Qt::CrossCursor); +#endif +} + +void PaintArea::setBrushColor(const QColor &color) +{ + this->color = color; +} + +void PaintArea::setBrushWidth(int width) +{ + thickness = width; +} + +//! [0] +void PaintArea::setBrush(BrushInterface *brushInterface, const QString &brush) +{ + this->brushInterface = brushInterface; + this->brush = brush; +} +//! [0] + +QSize PaintArea::sizeHint() const +{ + return theImage.size(); +} + +void PaintArea::paintEvent(QPaintEvent * /* event */) +{ + QPainter painter(this); + painter.drawImage(QPoint(0, 0), theImage); +} + +void PaintArea::mousePressEvent(QMouseEvent *event) +{ + if (event->button() == Qt::LeftButton) { + if (!pendingPath.isEmpty()) { + QPainter painter(&theImage); + setupPainter(painter); + + const QRectF boundingRect = pendingPath.boundingRect(); + QLinearGradient gradient(boundingRect.topRight(), + boundingRect.bottomLeft()); + gradient.setColorAt(0.0, QColor(color.red(), color.green(), + color.blue(), 63)); + gradient.setColorAt(1.0, QColor(color.red(), color.green(), + color.blue(), 191)); + painter.setBrush(gradient); + painter.translate(event->pos() - boundingRect.center()); + painter.drawPath(pendingPath); + + pendingPath = QPainterPath(); +#ifndef QT_NO_CURSOR + unsetCursor(); +#endif + update(); + } else { + if (brushInterface) { + QPainter painter(&theImage); + setupPainter(painter); + const QRect rect = brushInterface->mousePress(brush, painter, + event->pos()); + update(rect); + } + + lastPos = event->pos(); + } + } +} + +//! [1] +void PaintArea::mouseMoveEvent(QMouseEvent *event) +{ + if ((event->buttons() & Qt::LeftButton) && lastPos != QPoint(-1, -1)) { + if (brushInterface) { + QPainter painter(&theImage); + setupPainter(painter); + const QRect rect = brushInterface->mouseMove(brush, painter, lastPos, + event->pos()); + update(rect); + } + + lastPos = event->pos(); + } +} +//! [1] + +void PaintArea::mouseReleaseEvent(QMouseEvent *event) +{ + if (event->button() == Qt::LeftButton && lastPos != QPoint(-1, -1)) { + if (brushInterface) { + QPainter painter(&theImage); + setupPainter(painter); + QRect rect = brushInterface->mouseRelease(brush, painter, + event->pos()); + update(rect); + } + + lastPos = QPoint(-1, -1); + } +} + +void PaintArea::setupPainter(QPainter &painter) +{ + painter.setRenderHint(QPainter::Antialiasing, true); + painter.setPen(QPen(color, thickness, Qt::SolidLine, Qt::RoundCap, + Qt::RoundJoin)); +} diff --git a/examples/widgets/tools/plugandpaint/paintarea.h b/examples/widgets/tools/plugandpaint/paintarea.h new file mode 100644 index 0000000000..1bc0f810e0 --- /dev/null +++ b/examples/widgets/tools/plugandpaint/paintarea.h @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PAINTAREA_H +#define PAINTAREA_H + +#include <QColor> +#include <QImage> +#include <QPainterPath> +#include <QWidget> + +class BrushInterface; + +class PaintArea : public QWidget +{ + Q_OBJECT + +public: + PaintArea(QWidget *parent = 0); + + bool openImage(const QString &fileName); + bool saveImage(const QString &fileName, const char *fileFormat); + void setImage(const QImage &image); + void insertShape(const QPainterPath &path); + void setBrushColor(const QColor &color); + void setBrushWidth(int width); + void setBrush(BrushInterface *brushInterface, const QString &brush); + + QImage image() const { return theImage; } + QColor brushColor() const { return color; } + int brushWidth() const { return thickness; } + QSize sizeHint() const; + +protected: + void paintEvent(QPaintEvent *event); + void mousePressEvent(QMouseEvent *event); + void mouseMoveEvent(QMouseEvent *event); + void mouseReleaseEvent(QMouseEvent *event); + +private: + void setupPainter(QPainter &painter); + + QImage theImage; + QColor color; + int thickness; + + BrushInterface *brushInterface; + QString brush; + QPoint lastPos; + + QPainterPath pendingPath; +}; + +#endif diff --git a/examples/widgets/tools/plugandpaint/plugandpaint.desktop b/examples/widgets/tools/plugandpaint/plugandpaint.desktop new file mode 100644 index 0000000000..e39d512225 --- /dev/null +++ b/examples/widgets/tools/plugandpaint/plugandpaint.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=Plug appnameplaceholder Paint +Exec=/opt/usr/bin/plugandpaint +Icon=plugandpaint +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tools/plugandpaint/plugandpaint.pro b/examples/widgets/tools/plugandpaint/plugandpaint.pro new file mode 100644 index 0000000000..2c8c5117ee --- /dev/null +++ b/examples/widgets/tools/plugandpaint/plugandpaint.pro @@ -0,0 +1,25 @@ +#! [0] +HEADERS = interfaces.h \ + mainwindow.h \ + paintarea.h \ + plugindialog.h +SOURCES = main.cpp \ + mainwindow.cpp \ + paintarea.cpp \ + plugindialog.cpp + +LIBS = -L$${QT_BUILD_TREE}/examples/widgets/tools/plugandpaint/plugins -lpnp_basictools + +if(!debug_and_release|build_pass):CONFIG(debug, debug|release) { + mac:LIBS = $$member(LIBS, 0) $$member(LIBS, 1)_debug + win32:LIBS = $$member(LIBS, 0) $$member(LIBS, 1)d +} +#! [0] + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tools/plugandpaint +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS plugandpaint.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/tools/plugandpaint +INSTALLS += target sources + +QT += widgets diff --git a/examples/widgets/tools/plugandpaint/plugindialog.cpp b/examples/widgets/tools/plugandpaint/plugindialog.cpp new file mode 100644 index 0000000000..597f90c349 --- /dev/null +++ b/examples/widgets/tools/plugandpaint/plugindialog.cpp @@ -0,0 +1,156 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include "interfaces.h" +#include "plugindialog.h" + +#include <QPluginLoader> +#include <QStringList> +#include <QDir> + +#include <QLabel> +#include <QGridLayout> +#include <QPushButton> +#include <QTreeWidget> +#include <QTreeWidgetItem> +#include <QHeaderView> + +PluginDialog::PluginDialog(const QString &path, const QStringList &fileNames, + QWidget *parent) : + QDialog(parent), + label(new QLabel), + treeWidget(new QTreeWidget), + okButton(new QPushButton(tr("OK"))) +{ + treeWidget->setAlternatingRowColors(false); + treeWidget->setSelectionMode(QAbstractItemView::NoSelection); + treeWidget->setColumnCount(1); + treeWidget->header()->hide(); + + okButton->setDefault(true); + + connect(okButton, SIGNAL(clicked()), this, SLOT(close())); + + QGridLayout *mainLayout = new QGridLayout; + mainLayout->setColumnStretch(0, 1); + mainLayout->setColumnStretch(2, 1); + mainLayout->addWidget(label, 0, 0, 1, 3); + mainLayout->addWidget(treeWidget, 1, 0, 1, 3); + mainLayout->addWidget(okButton, 2, 1); + setLayout(mainLayout); + + interfaceIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirOpenIcon), + QIcon::Normal, QIcon::On); + interfaceIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirClosedIcon), + QIcon::Normal, QIcon::Off); + featureIcon.addPixmap(style()->standardPixmap(QStyle::SP_FileIcon)); + + setWindowTitle(tr("Plugin Information")); + findPlugins(path, fileNames); +} + +//! [0] +void PluginDialog::findPlugins(const QString &path, + const QStringList &fileNames) +{ + label->setText(tr("Plug & Paint found the following plugins\n" + "(looked in %1):") + .arg(QDir::toNativeSeparators(path))); + + const QDir dir(path); + + foreach (QObject *plugin, QPluginLoader::staticInstances()) + populateTreeWidget(plugin, tr("%1 (Static Plugin)") + .arg(plugin->metaObject()->className())); + + foreach (QString fileName, fileNames) { + QPluginLoader loader(dir.absoluteFilePath(fileName)); + QObject *plugin = loader.instance(); + if (plugin) + populateTreeWidget(plugin, fileName); + } +} +//! [0] + +//! [1] +void PluginDialog::populateTreeWidget(QObject *plugin, const QString &text) +{ + QTreeWidgetItem *pluginItem = new QTreeWidgetItem(treeWidget); + pluginItem->setText(0, text); + treeWidget->setItemExpanded(pluginItem, true); + + QFont boldFont = pluginItem->font(0); + boldFont.setBold(true); + pluginItem->setFont(0, boldFont); + + if (plugin) { + BrushInterface *iBrush = qobject_cast<BrushInterface *>(plugin); + if (iBrush) + addItems(pluginItem, "BrushInterface", iBrush->brushes()); + + ShapeInterface *iShape = qobject_cast<ShapeInterface *>(plugin); + if (iShape) + addItems(pluginItem, "ShapeInterface", iShape->shapes()); + + FilterInterface *iFilter = + qobject_cast<FilterInterface *>(plugin); + if (iFilter) + addItems(pluginItem, "FilterInterface", iFilter->filters()); + } +} +//! [1] + +void PluginDialog::addItems(QTreeWidgetItem *pluginItem, + const char *interfaceName, + const QStringList &features) +{ + QTreeWidgetItem *interfaceItem = new QTreeWidgetItem(pluginItem); + interfaceItem->setText(0, interfaceName); + interfaceItem->setIcon(0, interfaceIcon); + + foreach (QString feature, features) { + if (feature.endsWith("...")) + feature.chop(3); + QTreeWidgetItem *featureItem = new QTreeWidgetItem(interfaceItem); + featureItem->setText(0, feature); + featureItem->setIcon(0, featureIcon); + } +} diff --git a/examples/widgets/tools/plugandpaint/plugindialog.h b/examples/widgets/tools/plugandpaint/plugindialog.h new file mode 100644 index 0000000000..ff4a5d9c09 --- /dev/null +++ b/examples/widgets/tools/plugandpaint/plugindialog.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PLUGINDIALOG_H +#define PLUGINDIALOG_H + +#include <QDialog> +#include <QIcon> + +QT_BEGIN_NAMESPACE +class QLabel; +class QPushButton; +class QStringList; +class QTreeWidget; +class QTreeWidgetItem; +QT_END_NAMESPACE + +class PluginDialog : public QDialog +{ + Q_OBJECT + +public: + PluginDialog(const QString &path, const QStringList &fileNames, + QWidget *parent = 0); + +private: + void findPlugins(const QString &path, const QStringList &fileNames); + void populateTreeWidget(QObject *plugin, const QString &text); + void addItems(QTreeWidgetItem *pluginItem, const char *interfaceName, + const QStringList &features); + + QLabel *label; + QTreeWidget *treeWidget; + QPushButton *okButton; + QIcon interfaceIcon; + QIcon featureIcon; +}; + +#endif diff --git a/examples/widgets/tools/plugandpaintplugins/basictools/basictools.json b/examples/widgets/tools/plugandpaintplugins/basictools/basictools.json new file mode 100644 index 0000000000..0967ef424b --- /dev/null +++ b/examples/widgets/tools/plugandpaintplugins/basictools/basictools.json @@ -0,0 +1 @@ +{} diff --git a/examples/widgets/tools/plugandpaintplugins/basictools/basictools.pro b/examples/widgets/tools/plugandpaintplugins/basictools/basictools.pro new file mode 100644 index 0000000000..3190e45c46 --- /dev/null +++ b/examples/widgets/tools/plugandpaintplugins/basictools/basictools.pro @@ -0,0 +1,18 @@ +#! [0] +TEMPLATE = lib +CONFIG += plugin static +INCLUDEPATH += ../.. +HEADERS = basictoolsplugin.h +SOURCES = basictoolsplugin.cpp +OTHER_FILES += basictools.json +TARGET = $$qtLibraryTarget(pnp_basictools) +DESTDIR = ../../plugandpaint/plugins +#! [0] + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tools/plugandpaint/plugins +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS basictools.pro basictools.json +sources.path = $$[QT_INSTALL_EXAMPLES]/tools/plugandpaintplugins/basictools +INSTALLS += target sources + +QT += widgets diff --git a/examples/widgets/tools/plugandpaintplugins/basictools/basictoolsplugin.cpp b/examples/widgets/tools/plugandpaintplugins/basictools/basictoolsplugin.cpp new file mode 100644 index 0000000000..e0d68bac45 --- /dev/null +++ b/examples/widgets/tools/plugandpaintplugins/basictools/basictoolsplugin.cpp @@ -0,0 +1,191 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include <math.h> +#include <stdlib.h> + +#include "basictoolsplugin.h" + +const float Pi = 3.14159f; + +//! [0] +QStringList BasicToolsPlugin::brushes() const +{ + return QStringList() << tr("Pencil") << tr("Air Brush") + << tr("Random Letters"); +} +//! [0] + +//! [1] +QRect BasicToolsPlugin::mousePress(const QString &brush, QPainter &painter, + const QPoint &pos) +{ + return mouseMove(brush, painter, pos, pos); +} +//! [1] + +//! [2] +QRect BasicToolsPlugin::mouseMove(const QString &brush, QPainter &painter, + const QPoint &oldPos, const QPoint &newPos) +{ + painter.save(); + + int rad = painter.pen().width() / 2; + QRect boundingRect = QRect(oldPos, newPos).normalized() + .adjusted(-rad, -rad, +rad, +rad); + QColor color = painter.pen().color(); + int thickness = painter.pen().width(); + QColor transparentColor(color.red(), color.green(), color.blue(), 0); +//! [2] //! [3] + + if (brush == tr("Pencil")) { + painter.drawLine(oldPos, newPos); + } else if (brush == tr("Air Brush")) { + int numSteps = 2 + (newPos - oldPos).manhattanLength() / 2; + + painter.setBrush(QBrush(color, Qt::Dense6Pattern)); + painter.setPen(Qt::NoPen); + + for (int i = 0; i < numSteps; ++i) { + int x = oldPos.x() + i * (newPos.x() - oldPos.x()) / (numSteps - 1); + int y = oldPos.y() + i * (newPos.y() - oldPos.y()) / (numSteps - 1); + + painter.drawEllipse(x - (thickness / 2), y - (thickness / 2), + thickness, thickness); + } + } else if (brush == tr("Random Letters")) { + QChar ch('A' + (qrand() % 26)); + + QFont biggerFont = painter.font(); + biggerFont.setBold(true); + biggerFont.setPointSize(biggerFont.pointSize() + thickness); + painter.setFont(biggerFont); + + painter.drawText(newPos, QString(ch)); + + QFontMetrics metrics(painter.font()); + boundingRect = metrics.boundingRect(ch); + boundingRect.translate(newPos); + boundingRect.adjust(-10, -10, +10, +10); + } + painter.restore(); + return boundingRect; +} +//! [3] + +//! [4] +QRect BasicToolsPlugin::mouseRelease(const QString & /* brush */, + QPainter & /* painter */, + const QPoint & /* pos */) +{ + return QRect(0, 0, 0, 0); +} +//! [4] + +//! [5] +QStringList BasicToolsPlugin::shapes() const +{ + return QStringList() << tr("Circle") << tr("Star") << tr("Text..."); +} +//! [5] + +//! [6] +QPainterPath BasicToolsPlugin::generateShape(const QString &shape, + QWidget *parent) +{ + QPainterPath path; + + if (shape == tr("Circle")) { + path.addEllipse(0, 0, 50, 50); + } else if (shape == tr("Star")) { + path.moveTo(90, 50); + for (int i = 1; i < 5; ++i) { + path.lineTo(50 + 40 * cos(0.8 * i * Pi), + 50 + 40 * sin(0.8 * i * Pi)); + } + path.closeSubpath(); + } else if (shape == tr("Text...")) { + QString text = QInputDialog::getText(parent, tr("Text Shape"), + tr("Enter text:"), + QLineEdit::Normal, tr("Qt")); + if (!text.isEmpty()) { + QFont timesFont("Times", 50); + timesFont.setStyleStrategy(QFont::ForceOutline); + path.addText(0, 0, timesFont, text); + } + } + + return path; +} +//! [6] + +//! [7] +QStringList BasicToolsPlugin::filters() const +{ + return QStringList() << tr("Invert Pixels") << tr("Swap RGB") + << tr("Grayscale"); +} +//! [7] + +//! [8] +QImage BasicToolsPlugin::filterImage(const QString &filter, const QImage &image, + QWidget * /* parent */) +{ + QImage result = image.convertToFormat(QImage::Format_RGB32); + + if (filter == tr("Invert Pixels")) { + result.invertPixels(); + } else if (filter == tr("Swap RGB")) { + result = result.rgbSwapped(); + } else if (filter == tr("Grayscale")) { + for (int y = 0; y < result.height(); ++y) { + for (int x = 0; x < result.width(); ++x) { + int pixel = result.pixel(x, y); + int gray = qGray(pixel); + int alpha = qAlpha(pixel); + result.setPixel(x, y, qRgba(gray, gray, gray, alpha)); + } + } + } + return result; +} +//! [8] diff --git a/examples/widgets/tools/plugandpaintplugins/basictools/basictoolsplugin.h b/examples/widgets/tools/plugandpaintplugins/basictools/basictoolsplugin.h new file mode 100644 index 0000000000..b5159803f6 --- /dev/null +++ b/examples/widgets/tools/plugandpaintplugins/basictools/basictoolsplugin.h @@ -0,0 +1,89 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef BASICTOOLSPLUGIN_H +#define BASICTOOLSPLUGIN_H + +#include <QRect> +#include <QObject> +#include <QtPlugin> +#include <QStringList> +#include <QPainterPath> +#include <QImage> + +//! [0] +#include <plugandpaint/interfaces.h> + +//! [1] +class BasicToolsPlugin : public QObject, + public BrushInterface, + public ShapeInterface, + public FilterInterface +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "org.qt-project.Qt.Examples.PlugAndPaint.BrushInterface" FILE "basictools.json") + Q_INTERFACES(BrushInterface ShapeInterface FilterInterface) +//! [0] + +//! [2] +public: +//! [1] + // BrushInterface + QStringList brushes() const; + QRect mousePress(const QString &brush, QPainter &painter, + const QPoint &pos); + QRect mouseMove(const QString &brush, QPainter &painter, + const QPoint &oldPos, const QPoint &newPos); + QRect mouseRelease(const QString &brush, QPainter &painter, + const QPoint &pos); + + // ShapeInterface + QStringList shapes() const; + QPainterPath generateShape(const QString &shape, QWidget *parent); + + // FilterInterface + QStringList filters() const; + QImage filterImage(const QString &filter, const QImage &image, + QWidget *parent); +//! [3] +}; +//! [2] //! [3] + +#endif diff --git a/examples/widgets/tools/plugandpaintplugins/extrafilters/extrafilters.json b/examples/widgets/tools/plugandpaintplugins/extrafilters/extrafilters.json new file mode 100644 index 0000000000..0967ef424b --- /dev/null +++ b/examples/widgets/tools/plugandpaintplugins/extrafilters/extrafilters.json @@ -0,0 +1 @@ +{} diff --git a/examples/widgets/tools/plugandpaintplugins/extrafilters/extrafilters.pro b/examples/widgets/tools/plugandpaintplugins/extrafilters/extrafilters.pro new file mode 100644 index 0000000000..bc7ccffc0a --- /dev/null +++ b/examples/widgets/tools/plugandpaintplugins/extrafilters/extrafilters.pro @@ -0,0 +1,19 @@ +#! [0] +TEMPLATE = lib +CONFIG += plugin +INCLUDEPATH += ../.. +HEADERS = extrafiltersplugin.h +SOURCES = extrafiltersplugin.cpp +OTHER_FILES += extrafilters.json +TARGET = $$qtLibraryTarget(pnp_extrafilters) +DESTDIR = ../../plugandpaint/plugins + +#! [0] +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tools/plugandpaint/plugins +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS extrafilters.pro extrafilters.json +sources.path = $$[QT_INSTALL_EXAMPLES]/tools/plugandpaintplugins/extrafilters +INSTALLS += target sources + + +QT += widgets diff --git a/examples/widgets/tools/plugandpaintplugins/extrafilters/extrafiltersplugin.cpp b/examples/widgets/tools/plugandpaintplugins/extrafilters/extrafiltersplugin.cpp new file mode 100644 index 0000000000..297f164428 --- /dev/null +++ b/examples/widgets/tools/plugandpaintplugins/extrafilters/extrafiltersplugin.cpp @@ -0,0 +1,122 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include <math.h> +#include <stdlib.h> + +#include "extrafiltersplugin.h" + +QStringList ExtraFiltersPlugin::filters() const +{ + return QStringList() << tr("Flip Horizontally") << tr("Flip Vertically") + << tr("Smudge...") << tr("Threshold..."); +} + +QImage ExtraFiltersPlugin::filterImage(const QString &filter, + const QImage &image, QWidget *parent) +{ + QImage original = image.convertToFormat(QImage::Format_RGB32); + QImage result = original; + + if (filter == tr("Flip Horizontally")) { + for (int y = 0; y < original.height(); ++y) { + for (int x = 0; x < original.width(); ++x) { + int pixel = original.pixel(original.width() - x - 1, y); + result.setPixel(x, y, pixel); + } + } + } else if (filter == tr("Flip Vertically")) { + for (int y = 0; y < original.height(); ++y) { + for (int x = 0; x < original.width(); ++x) { + int pixel = original.pixel(x, original.height() - y - 1); + result.setPixel(x, y, pixel); + } + } + } else if (filter == tr("Smudge...")) { + bool ok; + int numIters = QInputDialog::getInt(parent, tr("Smudge Filter"), + tr("Enter number of iterations:"), + 5, 1, 20, 1, &ok); + if (ok) { + for (int i = 0; i < numIters; ++i) { + for (int y = 1; y < original.height() - 1; ++y) { + for (int x = 1; x < original.width() - 1; ++x) { + int p1 = original.pixel(x, y); + int p2 = original.pixel(x, y + 1); + int p3 = original.pixel(x, y - 1); + int p4 = original.pixel(x + 1, y); + int p5 = original.pixel(x - 1, y); + + int red = (qRed(p1) + qRed(p2) + qRed(p3) + qRed(p4) + + qRed(p5)) / 5; + int green = (qGreen(p1) + qGreen(p2) + qGreen(p3) + + qGreen(p4) + qGreen(p5)) / 5; + int blue = (qBlue(p1) + qBlue(p2) + qBlue(p3) + + qBlue(p4) + qBlue(p5)) / 5; + int alpha = (qAlpha(p1) + qAlpha(p2) + qAlpha(p3) + + qAlpha(p4) + qAlpha(p5)) / 5; + + result.setPixel(x, y, qRgba(red, green, blue, alpha)); + } + } + } + } + } else if (filter == tr("Threshold...")) { + bool ok; + int threshold = QInputDialog::getInt(parent, tr("Threshold Filter"), + tr("Enter threshold:"), + 10, 1, 256, 1, &ok); + if (ok) { + int factor = 256 / threshold; + for (int y = 0; y < original.height(); ++y) { + for (int x = 0; x < original.width(); ++x) { + int pixel = original.pixel(x, y); + result.setPixel(x, y, qRgba(qRed(pixel) / factor * factor, + qGreen(pixel) / factor * factor, + qBlue(pixel) / factor * factor, + qAlpha(pixel))); + } + } + } + } + return result; +} diff --git a/examples/widgets/tools/plugandpaintplugins/extrafilters/extrafiltersplugin.h b/examples/widgets/tools/plugandpaintplugins/extrafilters/extrafiltersplugin.h new file mode 100644 index 0000000000..7add7abb53 --- /dev/null +++ b/examples/widgets/tools/plugandpaintplugins/extrafilters/extrafiltersplugin.h @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef EXTRAFILTERSPLUGIN_H +#define EXTRAFILTERSPLUGIN_H + +//! [0] +#include <QObject> +#include <QtPlugin> +#include <QStringList> +#include <QImage> + +#include <plugandpaint/interfaces.h> + +class ExtraFiltersPlugin : public QObject, public FilterInterface +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "org.qt-project.Qt.Examples.PlugAndPaint.FilterInterface" FILE "extrafilters.json") + Q_INTERFACES(FilterInterface) + +public: + QStringList filters() const; + QImage filterImage(const QString &filter, const QImage &image, + QWidget *parent); +}; +//! [0] + +#endif diff --git a/examples/widgets/tools/plugandpaintplugins/plugandpaintplugins.pro b/examples/widgets/tools/plugandpaintplugins/plugandpaintplugins.pro new file mode 100644 index 0000000000..8eab48e92d --- /dev/null +++ b/examples/widgets/tools/plugandpaintplugins/plugandpaintplugins.pro @@ -0,0 +1,11 @@ +TEMPLATE = subdirs +SUBDIRS = basictools \ + extrafilters + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tools/plugandpaintplugins +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS plugandpaintplugins.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/tools/plugandpaintplugins +INSTALLS += target sources + +QT += widgets diff --git a/examples/widgets/tools/regexp/main.cpp b/examples/widgets/tools/regexp/main.cpp new file mode 100644 index 0000000000..f48a6444f1 --- /dev/null +++ b/examples/widgets/tools/regexp/main.cpp @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> + +#include "regexpdialog.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + RegExpDialog dialog; + dialog.show(); + return app.exec(); +} diff --git a/examples/widgets/tools/regexp/regexp.desktop b/examples/widgets/tools/regexp/regexp.desktop new file mode 100644 index 0000000000..b3ae14e360 --- /dev/null +++ b/examples/widgets/tools/regexp/regexp.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=Regular Expressions +Exec=/opt/usr/bin/regexp +Icon=regexp +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tools/regexp/regexp.pro b/examples/widgets/tools/regexp/regexp.pro new file mode 100644 index 0000000000..8e88511ead --- /dev/null +++ b/examples/widgets/tools/regexp/regexp.pro @@ -0,0 +1,13 @@ +HEADERS = regexpdialog.h +SOURCES = regexpdialog.cpp \ + main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tools/regexp +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS regexp.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/tools/regexp +INSTALLS += target sources + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tools/regexp/regexpdialog.cpp b/examples/widgets/tools/regexp/regexpdialog.cpp new file mode 100644 index 0000000000..178433ee34 --- /dev/null +++ b/examples/widgets/tools/regexp/regexpdialog.cpp @@ -0,0 +1,188 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include "regexpdialog.h" + +RegExpDialog::RegExpDialog(QWidget *parent) + : QDialog(parent) +{ + patternComboBox = new QComboBox; + patternComboBox->setEditable(true); + patternComboBox->setSizePolicy(QSizePolicy::Expanding, + QSizePolicy::Preferred); + + patternLabel = new QLabel(tr("&Pattern:")); + patternLabel->setBuddy(patternComboBox); + + escapedPatternLineEdit = new QLineEdit; + escapedPatternLineEdit->setReadOnly(true); + QPalette palette = escapedPatternLineEdit->palette(); + palette.setBrush(QPalette::Base, + palette.brush(QPalette::Disabled, QPalette::Base)); + escapedPatternLineEdit->setPalette(palette); + + escapedPatternLabel = new QLabel(tr("&Escaped Pattern:")); + escapedPatternLabel->setBuddy(escapedPatternLineEdit); + + syntaxComboBox = new QComboBox; + syntaxComboBox->addItem(tr("Regular expression v1"), QRegExp::RegExp); + syntaxComboBox->addItem(tr("Regular expression v2"), QRegExp::RegExp2); + syntaxComboBox->addItem(tr("Wildcard"), QRegExp::Wildcard); + syntaxComboBox->addItem(tr("Fixed string"), QRegExp::FixedString); + syntaxComboBox->addItem(tr("W3C Xml Schema 1.1"), QRegExp::W3CXmlSchema11); + + syntaxLabel = new QLabel(tr("&Pattern Syntax:")); + syntaxLabel->setBuddy(syntaxComboBox); + + textComboBox = new QComboBox; + textComboBox->setEditable(true); + textComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); + + textLabel = new QLabel(tr("&Text:")); + textLabel->setBuddy(textComboBox); + + caseSensitiveCheckBox = new QCheckBox(tr("Case &Sensitive")); + caseSensitiveCheckBox->setChecked(true); + minimalCheckBox = new QCheckBox(tr("&Minimal")); + + indexLabel = new QLabel(tr("Index of Match:")); + indexEdit = new QLineEdit; + indexEdit->setReadOnly(true); + + matchedLengthLabel = new QLabel(tr("Matched Length:")); + matchedLengthEdit = new QLineEdit; + matchedLengthEdit->setReadOnly(true); + + for (int i = 0; i < MaxCaptures; ++i) { + captureLabels[i] = new QLabel(tr("Capture %1:").arg(i)); + captureEdits[i] = new QLineEdit; + captureEdits[i]->setReadOnly(true); + } + captureLabels[0]->setText(tr("Match:")); + + QHBoxLayout *checkBoxLayout = new QHBoxLayout; + checkBoxLayout->addWidget(caseSensitiveCheckBox); + checkBoxLayout->addWidget(minimalCheckBox); + checkBoxLayout->addStretch(1); + + QGridLayout *mainLayout = new QGridLayout; + mainLayout->addWidget(patternLabel, 0, 0); + mainLayout->addWidget(patternComboBox, 0, 1); + mainLayout->addWidget(escapedPatternLabel, 1, 0); + mainLayout->addWidget(escapedPatternLineEdit, 1, 1); + mainLayout->addWidget(syntaxLabel, 2, 0); + mainLayout->addWidget(syntaxComboBox, 2, 1); + mainLayout->addLayout(checkBoxLayout, 3, 0, 1, 2); + mainLayout->addWidget(textLabel, 4, 0); + mainLayout->addWidget(textComboBox, 4, 1); + mainLayout->addWidget(indexLabel, 5, 0); + mainLayout->addWidget(indexEdit, 5, 1); + mainLayout->addWidget(matchedLengthLabel, 6, 0); + mainLayout->addWidget(matchedLengthEdit, 6, 1); + + for (int j = 0; j < MaxCaptures; ++j) { + mainLayout->addWidget(captureLabels[j], 7 + j, 0); + mainLayout->addWidget(captureEdits[j], 7 + j, 1); + } + setLayout(mainLayout); + + connect(patternComboBox, SIGNAL(editTextChanged(QString)), + this, SLOT(refresh())); + connect(textComboBox, SIGNAL(editTextChanged(QString)), + this, SLOT(refresh())); + connect(caseSensitiveCheckBox, SIGNAL(toggled(bool)), + this, SLOT(refresh())); + connect(minimalCheckBox, SIGNAL(toggled(bool)), this, SLOT(refresh())); + connect(syntaxComboBox, SIGNAL(currentIndexChanged(int)), + this, SLOT(refresh())); + + patternComboBox->addItem(tr("[A-Za-z_]+([A-Za-z_0-9]*)")); + textComboBox->addItem(tr("(10 + delta4) * 32")); + + setWindowTitle(tr("RegExp")); + setFixedHeight(sizeHint().height()); + refresh(); +} + +void RegExpDialog::refresh() +{ + setUpdatesEnabled(false); + + QString pattern = patternComboBox->currentText(); + QString text = textComboBox->currentText(); + + QString escaped = pattern; + escaped.replace("\\", "\\\\"); + escaped.replace("\"", "\\\""); + escaped.prepend("\""); + escaped.append("\""); + escapedPatternLineEdit->setText(escaped); + + QRegExp rx(pattern); + Qt::CaseSensitivity cs = Qt::CaseInsensitive; + if (caseSensitiveCheckBox->isChecked()) + cs = Qt::CaseSensitive; + rx.setCaseSensitivity(cs); + rx.setMinimal(minimalCheckBox->isChecked()); + QRegExp::PatternSyntax syntax = QRegExp::PatternSyntax( + syntaxComboBox->itemData(syntaxComboBox->currentIndex()).toInt()); + rx.setPatternSyntax(syntax); + + QPalette palette = patternComboBox->palette(); + if (rx.isValid()) { + palette.setColor(QPalette::Text, + textComboBox->palette().color(QPalette::Text)); + } else { + palette.setColor(QPalette::Text, Qt::red); + } + patternComboBox->setPalette(palette); + + indexEdit->setText(QString::number(rx.indexIn(text))); + matchedLengthEdit->setText(QString::number(rx.matchedLength())); + for (int i = 0; i < MaxCaptures; ++i) { + captureLabels[i]->setEnabled(i <= rx.captureCount()); + captureEdits[i]->setEnabled(i <= rx.captureCount()); + captureEdits[i]->setText(rx.cap(i)); + } + + setUpdatesEnabled(true); +} diff --git a/examples/widgets/tools/regexp/regexpdialog.h b/examples/widgets/tools/regexp/regexpdialog.h new file mode 100644 index 0000000000..df730586bc --- /dev/null +++ b/examples/widgets/tools/regexp/regexpdialog.h @@ -0,0 +1,85 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef REGEXPDIALOG_H +#define REGEXPDIALOG_H + +#include <QDialog> + +QT_BEGIN_NAMESPACE +class QCheckBox; +class QComboBox; +class QLabel; +class QLineEdit; +QT_END_NAMESPACE + +class RegExpDialog : public QDialog +{ + Q_OBJECT + +public: + RegExpDialog(QWidget *parent = 0); + +private slots: + void refresh(); + +private: + QLabel *patternLabel; + QLabel *escapedPatternLabel; + QLabel *syntaxLabel; + QLabel *textLabel; + QComboBox *patternComboBox; + QLineEdit *escapedPatternLineEdit; + QComboBox *textComboBox; + QCheckBox *caseSensitiveCheckBox; + QCheckBox *minimalCheckBox; + QComboBox *syntaxComboBox; + + QLabel *indexLabel; + QLabel *matchedLengthLabel; + QLineEdit *indexEdit; + QLineEdit *matchedLengthEdit; + + enum { MaxCaptures = 6 }; + QLabel *captureLabels[MaxCaptures]; + QLineEdit *captureEdits[MaxCaptures]; +}; + +#endif diff --git a/examples/widgets/tools/settingseditor/inifiles/licensepage.ini b/examples/widgets/tools/settingseditor/inifiles/licensepage.ini new file mode 100644 index 0000000000..608d1b7885 --- /dev/null +++ b/examples/widgets/tools/settingseditor/inifiles/licensepage.ini @@ -0,0 +1,46 @@ +[Field%201] +Bottom=89 +Flags=MULTILINE|VSCROLL|READONLY +Left=4 +Right=296 +State=No license agreement file found. Please contact support. +Top=14 +Type=Text + +[Field%202] +Bottom=8 +Left=4 +Right=294 +Text=Press Page Down to see the rest of the agreement. +Top=0 +Type=Label + +[Field%203] +Bottom=111 +Left=4 +Right=297 +Text=If you accept the terms of the agreement, select the first option below. You must accept the agreement to install this software. Click Next to continue. +Top=92 +Type=Label + +[Field%204] +Bottom=129 +Flags=GROUP|NOTIFY +Left=4 +Right=299 +Text=I &accept the terms in the License Agreement +Top=120 +Type=RadioButton + +[Field%205] +Bottom=140 +Flags=NOTIFY +Left=4 +Right=300 +State=1 +Text=I &do not accept the terms in the License Agreement +Top=129 +Type=RadioButton + +[Settings] +NumFields=5 diff --git a/examples/widgets/tools/settingseditor/inifiles/qsa.ini b/examples/widgets/tools/settingseditor/inifiles/qsa.ini new file mode 100644 index 0000000000..56a2964ee5 --- /dev/null +++ b/examples/widgets/tools/settingseditor/inifiles/qsa.ini @@ -0,0 +1,26 @@ +[Field%201] +Bottom=65 +Left=0 +Right=299 +Text=QSA Build Options +Top=9 +Type=Groupbox + +[Field%202] +Bottom=37 +Left=20 +Right=284 +Text=Don't compile QSA Workbench into QSA. +Top=27 +Type=Checkbox + +[Field%203] +Bottom=56 +Left=20 +Right=247 +Text=Don't compile QSA Workbench nor QSA Editor into QSA. +Top=45 +Type=Checkbox + +[Settings] +NumFields=3 diff --git a/examples/widgets/tools/settingseditor/locationdialog.cpp b/examples/widgets/tools/settingseditor/locationdialog.cpp new file mode 100644 index 0000000000..dd725a02cb --- /dev/null +++ b/examples/widgets/tools/settingseditor/locationdialog.cpp @@ -0,0 +1,216 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include "locationdialog.h" + +LocationDialog::LocationDialog(QWidget *parent) + : QDialog(parent) +{ + formatComboBox = new QComboBox; + formatComboBox->addItem(tr("Native")); + formatComboBox->addItem(tr("INI")); + + scopeComboBox = new QComboBox; + scopeComboBox->addItem(tr("User")); + scopeComboBox->addItem(tr("System")); + + organizationComboBox = new QComboBox; + organizationComboBox->addItem(tr("Qt")); + organizationComboBox->setEditable(true); + + applicationComboBox = new QComboBox; + applicationComboBox->addItem(tr("Any")); + applicationComboBox->addItem(tr("Application Example")); + applicationComboBox->addItem(tr("Assistant")); + applicationComboBox->addItem(tr("Designer")); + applicationComboBox->addItem(tr("Linguist")); + applicationComboBox->setEditable(true); + applicationComboBox->setCurrentIndex(3); + + formatLabel = new QLabel(tr("&Format:")); + formatLabel->setBuddy(formatComboBox); + + scopeLabel = new QLabel(tr("&Scope:")); + scopeLabel->setBuddy(scopeComboBox); + + organizationLabel = new QLabel(tr("&Organization:")); + organizationLabel->setBuddy(organizationComboBox); + + applicationLabel = new QLabel(tr("&Application:")); + applicationLabel->setBuddy(applicationComboBox); + + locationsGroupBox = new QGroupBox(tr("Setting Locations")); + + QStringList labels; + labels << tr("Location") << tr("Access"); + + locationsTable = new QTableWidget; + locationsTable->setSelectionMode(QAbstractItemView::SingleSelection); + locationsTable->setSelectionBehavior(QAbstractItemView::SelectRows); + locationsTable->setEditTriggers(QAbstractItemView::NoEditTriggers); + locationsTable->setColumnCount(2); + locationsTable->setHorizontalHeaderLabels(labels); + locationsTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); + locationsTable->horizontalHeader()->resizeSection(1, 180); + + buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok + | QDialogButtonBox::Cancel); + + connect(formatComboBox, SIGNAL(activated(int)), + this, SLOT(updateLocationsTable())); + connect(scopeComboBox, SIGNAL(activated(int)), + this, SLOT(updateLocationsTable())); + connect(organizationComboBox->lineEdit(), + SIGNAL(editingFinished()), + this, SLOT(updateLocationsTable())); + connect(applicationComboBox->lineEdit(), + SIGNAL(editingFinished()), + this, SLOT(updateLocationsTable())); + connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); + connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); + + QVBoxLayout *locationsLayout = new QVBoxLayout; + locationsLayout->addWidget(locationsTable); + locationsGroupBox->setLayout(locationsLayout); + + QGridLayout *mainLayout = new QGridLayout; + mainLayout->addWidget(formatLabel, 0, 0); + mainLayout->addWidget(formatComboBox, 0, 1); + mainLayout->addWidget(scopeLabel, 1, 0); + mainLayout->addWidget(scopeComboBox, 1, 1); + mainLayout->addWidget(organizationLabel, 2, 0); + mainLayout->addWidget(organizationComboBox, 2, 1); + mainLayout->addWidget(applicationLabel, 3, 0); + mainLayout->addWidget(applicationComboBox, 3, 1); + mainLayout->addWidget(locationsGroupBox, 4, 0, 1, 2); + mainLayout->addWidget(buttonBox, 5, 0, 1, 2); + setLayout(mainLayout); + + updateLocationsTable(); + + setWindowTitle(tr("Open Application Settings")); + resize(650, 400); +} + +QSettings::Format LocationDialog::format() const +{ + if (formatComboBox->currentIndex() == 0) + return QSettings::NativeFormat; + else + return QSettings::IniFormat; +} + +QSettings::Scope LocationDialog::scope() const +{ + if (scopeComboBox->currentIndex() == 0) + return QSettings::UserScope; + else + return QSettings::SystemScope; +} + +QString LocationDialog::organization() const +{ + return organizationComboBox->currentText(); +} + +QString LocationDialog::application() const +{ + if (applicationComboBox->currentText() == tr("Any")) + return ""; + else + return applicationComboBox->currentText(); +} + +void LocationDialog::updateLocationsTable() +{ + locationsTable->setUpdatesEnabled(false); + locationsTable->setRowCount(0); + + for (int i = 0; i < 2; ++i) { + if (i == 0 && scope() == QSettings::SystemScope) + continue; + + QSettings::Scope actualScope = (i == 0) ? QSettings::UserScope + : QSettings::SystemScope; + for (int j = 0; j < 2; ++j) { + if (j == 0 && application().isEmpty()) + continue; + + QString actualApplication; + if (j == 0) + actualApplication = application(); + QSettings settings(format(), actualScope, organization(), + actualApplication); + + int row = locationsTable->rowCount(); + locationsTable->setRowCount(row + 1); + + QTableWidgetItem *item0 = new QTableWidgetItem; + item0->setText(settings.fileName()); + + QTableWidgetItem *item1 = new QTableWidgetItem; + bool disable = (settings.childKeys().isEmpty() + && settings.childGroups().isEmpty()); + + if (row == 0) { + if (settings.isWritable()) { + item1->setText(tr("Read-write")); + disable = false; + } else { + item1->setText(tr("Read-only")); + } + buttonBox->button(QDialogButtonBox::Ok)->setDisabled(disable); + } else { + item1->setText(tr("Read-only fallback")); + } + + if (disable) { + item0->setFlags(item0->flags() & ~Qt::ItemIsEnabled); + item1->setFlags(item1->flags() & ~Qt::ItemIsEnabled); + } + + locationsTable->setItem(row, 0, item0); + locationsTable->setItem(row, 1, item1); + } + } + locationsTable->setUpdatesEnabled(true); +} diff --git a/examples/widgets/tools/settingseditor/locationdialog.h b/examples/widgets/tools/settingseditor/locationdialog.h new file mode 100644 index 0000000000..e9bf2bfbda --- /dev/null +++ b/examples/widgets/tools/settingseditor/locationdialog.h @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef LOCATIONDIALOG_H +#define LOCATIONDIALOG_H + +#include <QDialog> +#include <QSettings> + +QT_BEGIN_NAMESPACE +class QComboBox; +class QDialogButtonBox; +class QGroupBox; +class QLabel; +class QTableWidget; +QT_END_NAMESPACE + +class LocationDialog : public QDialog +{ + Q_OBJECT + +public: + LocationDialog(QWidget *parent = 0); + + QSettings::Format format() const; + QSettings::Scope scope() const; + QString organization() const; + QString application() const; + +private slots: + void updateLocationsTable(); + +private: + QLabel *formatLabel; + QLabel *scopeLabel; + QLabel *organizationLabel; + QLabel *applicationLabel; + QComboBox *formatComboBox; + QComboBox *scopeComboBox; + QComboBox *organizationComboBox; + QComboBox *applicationComboBox; + QGroupBox *locationsGroupBox; + QTableWidget *locationsTable; + QDialogButtonBox *buttonBox; +}; + +#endif diff --git a/examples/widgets/tools/settingseditor/main.cpp b/examples/widgets/tools/settingseditor/main.cpp new file mode 100644 index 0000000000..f03543e449 --- /dev/null +++ b/examples/widgets/tools/settingseditor/main.cpp @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + MainWindow mainWin; + mainWin.show(); + return app.exec(); +} diff --git a/examples/widgets/tools/settingseditor/mainwindow.cpp b/examples/widgets/tools/settingseditor/mainwindow.cpp new file mode 100644 index 0000000000..e899f6021f --- /dev/null +++ b/examples/widgets/tools/settingseditor/mainwindow.cpp @@ -0,0 +1,222 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include "locationdialog.h" +#include "mainwindow.h" +#include "settingstree.h" + +MainWindow::MainWindow() +{ + settingsTree = new SettingsTree; + setCentralWidget(settingsTree); + + locationDialog = 0; + + createActions(); + createMenus(); + + autoRefreshAct->setChecked(true); + fallbacksAct->setChecked(true); + + setWindowTitle(tr("Settings Editor")); + resize(500, 600); +} + +void MainWindow::openSettings() +{ + if (!locationDialog) + locationDialog = new LocationDialog(this); + + if (locationDialog->exec()) { + QSettings *settings = new QSettings(locationDialog->format(), + locationDialog->scope(), + locationDialog->organization(), + locationDialog->application()); + setSettingsObject(settings); + fallbacksAct->setEnabled(true); + } +} + +void MainWindow::openIniFile() +{ + QString fileName = QFileDialog::getOpenFileName(this, tr("Open INI File"), + "", tr("INI Files (*.ini *.conf)")); + if (!fileName.isEmpty()) { + QSettings *settings = new QSettings(fileName, QSettings::IniFormat); + setSettingsObject(settings); + fallbacksAct->setEnabled(false); + } +} + +void MainWindow::openPropertyList() +{ + QString fileName = QFileDialog::getOpenFileName(this, + tr("Open Property List"), + "", tr("Property List Files (*.plist)")); + if (!fileName.isEmpty()) { + QSettings *settings = new QSettings(fileName, QSettings::NativeFormat); + setSettingsObject(settings); + fallbacksAct->setEnabled(false); + } +} + +void MainWindow::openRegistryPath() +{ + QString path = QInputDialog::getText(this, tr("Open Registry Path"), + tr("Enter the path in the Windows registry:"), + QLineEdit::Normal, "HKEY_CURRENT_USER\\"); + if (!path.isEmpty()) { + QSettings *settings = new QSettings(path, QSettings::NativeFormat); + setSettingsObject(settings); + fallbacksAct->setEnabled(false); + } +} + +void MainWindow::about() +{ + QMessageBox::about(this, tr("About Settings Editor"), + tr("The <b>Settings Editor</b> example shows how to access " + "application settings using Qt.")); +} + +void MainWindow::createActions() +{ + openSettingsAct = new QAction(tr("&Open Application Settings..."), this); + openSettingsAct->setShortcuts(QKeySequence::Open); + connect(openSettingsAct, SIGNAL(triggered()), this, SLOT(openSettings())); + + openIniFileAct = new QAction(tr("Open I&NI File..."), this); + openIniFileAct->setShortcut(tr("Ctrl+N")); + connect(openIniFileAct, SIGNAL(triggered()), this, SLOT(openIniFile())); + + openPropertyListAct = new QAction(tr("Open Mac &Property List..."), this); + openPropertyListAct->setShortcut(tr("Ctrl+P")); + connect(openPropertyListAct, SIGNAL(triggered()), + this, SLOT(openPropertyList())); + + openRegistryPathAct = new QAction(tr("Open Windows &Registry Path..."), + this); + openRegistryPathAct->setShortcut(tr("Ctrl+G")); + connect(openRegistryPathAct, SIGNAL(triggered()), + this, SLOT(openRegistryPath())); + + refreshAct = new QAction(tr("&Refresh"), this); + refreshAct->setShortcut(tr("Ctrl+R")); + refreshAct->setEnabled(false); + connect(refreshAct, SIGNAL(triggered()), settingsTree, SLOT(refresh())); + + exitAct = new QAction(tr("E&xit"), this); + exitAct->setShortcuts(QKeySequence::Quit); + connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); + + autoRefreshAct = new QAction(tr("&Auto-Refresh"), this); + autoRefreshAct->setShortcut(tr("Ctrl+A")); + autoRefreshAct->setCheckable(true); + autoRefreshAct->setEnabled(false); + connect(autoRefreshAct, SIGNAL(triggered(bool)), + settingsTree, SLOT(setAutoRefresh(bool))); + connect(autoRefreshAct, SIGNAL(triggered(bool)), + refreshAct, SLOT(setDisabled(bool))); + + fallbacksAct = new QAction(tr("&Fallbacks"), this); + fallbacksAct->setShortcut(tr("Ctrl+F")); + fallbacksAct->setCheckable(true); + fallbacksAct->setEnabled(false); + connect(fallbacksAct, SIGNAL(triggered(bool)), + settingsTree, SLOT(setFallbacksEnabled(bool))); + + aboutAct = new QAction(tr("&About"), this); + connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); + + aboutQtAct = new QAction(tr("About &Qt"), this); + connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); + +#ifndef Q_OS_MAC + openPropertyListAct->setEnabled(false); +#endif +#ifndef Q_OS_WIN + openRegistryPathAct->setEnabled(false); +#endif +} + +void MainWindow::createMenus() +{ + fileMenu = menuBar()->addMenu(tr("&File")); + fileMenu->addAction(openSettingsAct); + fileMenu->addAction(openIniFileAct); + fileMenu->addAction(openPropertyListAct); + fileMenu->addAction(openRegistryPathAct); + fileMenu->addSeparator(); + fileMenu->addAction(refreshAct); + fileMenu->addSeparator(); + fileMenu->addAction(exitAct); + + optionsMenu = menuBar()->addMenu(tr("&Options")); + optionsMenu->addAction(autoRefreshAct); + optionsMenu->addAction(fallbacksAct); + + menuBar()->addSeparator(); + + helpMenu = menuBar()->addMenu(tr("&Help")); + helpMenu->addAction(aboutAct); + helpMenu->addAction(aboutQtAct); +} + +void MainWindow::setSettingsObject(QSettings *settings) +{ + settings->setFallbacksEnabled(fallbacksAct->isChecked()); + settingsTree->setSettingsObject(settings); + + refreshAct->setEnabled(true); + autoRefreshAct->setEnabled(true); + + QString niceName = settings->fileName(); + niceName.replace("\\", "/"); + int pos = niceName.lastIndexOf("/"); + if (pos != -1) + niceName.remove(0, pos + 1); + + if (!settings->isWritable()) + niceName = tr("%1 (read only)").arg(niceName); + + setWindowTitle(tr("%1 - %2").arg(niceName).arg(tr("Settings Editor"))); +} diff --git a/examples/widgets/tools/settingseditor/mainwindow.h b/examples/widgets/tools/settingseditor/mainwindow.h new file mode 100644 index 0000000000..febbbe9078 --- /dev/null +++ b/examples/widgets/tools/settingseditor/mainwindow.h @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> + +QT_BEGIN_NAMESPACE +class QAction; +class QMenu; +class QSettings; +QT_END_NAMESPACE +class LocationDialog; +class SettingsTree; + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +private slots: + void openSettings(); + void openIniFile(); + void openPropertyList(); + void openRegistryPath(); + void about(); + +private: + void createActions(); + void createMenus(); + void setSettingsObject(QSettings *settings); + + SettingsTree *settingsTree; + LocationDialog *locationDialog; + + QMenu *fileMenu; + QMenu *optionsMenu; + QMenu *helpMenu; + QAction *openSettingsAct; + QAction *openIniFileAct; + QAction *openPropertyListAct; + QAction *openRegistryPathAct; + QAction *refreshAct; + QAction *exitAct; + QAction *autoRefreshAct; + QAction *fallbacksAct; + QAction *aboutAct; + QAction *aboutQtAct; +}; + +#endif diff --git a/examples/widgets/tools/settingseditor/settingseditor.desktop b/examples/widgets/tools/settingseditor/settingseditor.desktop new file mode 100644 index 0000000000..b8561a6f3a --- /dev/null +++ b/examples/widgets/tools/settingseditor/settingseditor.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=Settings Editor +Exec=/opt/usr/bin/settingseditor +Icon=settingseditor +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tools/settingseditor/settingseditor.pro b/examples/widgets/tools/settingseditor/settingseditor.pro new file mode 100644 index 0000000000..2499b22436 --- /dev/null +++ b/examples/widgets/tools/settingseditor/settingseditor.pro @@ -0,0 +1,19 @@ +HEADERS = locationdialog.h \ + mainwindow.h \ + settingstree.h \ + variantdelegate.h +SOURCES = locationdialog.cpp \ + main.cpp \ + mainwindow.cpp \ + settingstree.cpp \ + variantdelegate.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tools/settingseditor +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS settingseditor.pro inifiles +sources.path = $$[QT_INSTALL_EXAMPLES]/tools/settingseditor +INSTALLS += target sources + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tools/settingseditor/settingstree.cpp b/examples/widgets/tools/settingseditor/settingstree.cpp new file mode 100644 index 0000000000..050473e777 --- /dev/null +++ b/examples/widgets/tools/settingseditor/settingstree.cpp @@ -0,0 +1,262 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include "settingstree.h" +#include "variantdelegate.h" + +SettingsTree::SettingsTree(QWidget *parent) + : QTreeWidget(parent) +{ + setItemDelegate(new VariantDelegate(this)); + + QStringList labels; + labels << tr("Setting") << tr("Type") << tr("Value"); + setHeaderLabels(labels); + header()->setSectionResizeMode(0, QHeaderView::Stretch); + header()->setSectionResizeMode(2, QHeaderView::Stretch); + + settings = 0; + refreshTimer.setInterval(2000); + autoRefresh = false; + + groupIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirClosedIcon), + QIcon::Normal, QIcon::Off); + groupIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirOpenIcon), + QIcon::Normal, QIcon::On); + keyIcon.addPixmap(style()->standardPixmap(QStyle::SP_FileIcon)); + + connect(&refreshTimer, SIGNAL(timeout()), this, SLOT(maybeRefresh())); +} + +void SettingsTree::setSettingsObject(QSettings *settings) +{ + delete this->settings; + this->settings = settings; + clear(); + + if (settings) { + settings->setParent(this); + refresh(); + if (autoRefresh) + refreshTimer.start(); + } else { + refreshTimer.stop(); + } +} + +QSize SettingsTree::sizeHint() const +{ + return QSize(800, 600); +} + +void SettingsTree::setAutoRefresh(bool autoRefresh) +{ + this->autoRefresh = autoRefresh; + if (settings) { + if (autoRefresh) { + maybeRefresh(); + refreshTimer.start(); + } else { + refreshTimer.stop(); + } + } +} + +void SettingsTree::setFallbacksEnabled(bool enabled) +{ + if (settings) { + settings->setFallbacksEnabled(enabled); + refresh(); + } +} + +void SettingsTree::maybeRefresh() +{ + if (state() != EditingState) + refresh(); +} + +void SettingsTree::refresh() +{ + if (!settings) + return; + + disconnect(this, SIGNAL(itemChanged(QTreeWidgetItem*,int)), + this, SLOT(updateSetting(QTreeWidgetItem*))); + + settings->sync(); + updateChildItems(0); + + connect(this, SIGNAL(itemChanged(QTreeWidgetItem*,int)), + this, SLOT(updateSetting(QTreeWidgetItem*))); +} + +bool SettingsTree::event(QEvent *event) +{ + if (event->type() == QEvent::WindowActivate) { + if (isActiveWindow() && autoRefresh) + maybeRefresh(); + } + return QTreeWidget::event(event); +} + +void SettingsTree::updateSetting(QTreeWidgetItem *item) +{ + QString key = item->text(0); + QTreeWidgetItem *ancestor = item->parent(); + while (ancestor) { + key.prepend(ancestor->text(0) + "/"); + ancestor = ancestor->parent(); + } + + settings->setValue(key, item->data(2, Qt::UserRole)); + if (autoRefresh) + refresh(); +} + +void SettingsTree::updateChildItems(QTreeWidgetItem *parent) +{ + int dividerIndex = 0; + + foreach (QString group, settings->childGroups()) { + QTreeWidgetItem *child; + int childIndex = findChild(parent, group, dividerIndex); + if (childIndex != -1) { + child = childAt(parent, childIndex); + child->setText(1, ""); + child->setText(2, ""); + child->setData(2, Qt::UserRole, QVariant()); + moveItemForward(parent, childIndex, dividerIndex); + } else { + child = createItem(group, parent, dividerIndex); + } + child->setIcon(0, groupIcon); + ++dividerIndex; + + settings->beginGroup(group); + updateChildItems(child); + settings->endGroup(); + } + + foreach (QString key, settings->childKeys()) { + QTreeWidgetItem *child; + int childIndex = findChild(parent, key, 0); + + if (childIndex == -1 || childIndex >= dividerIndex) { + if (childIndex != -1) { + child = childAt(parent, childIndex); + for (int i = 0; i < child->childCount(); ++i) + delete childAt(child, i); + moveItemForward(parent, childIndex, dividerIndex); + } else { + child = createItem(key, parent, dividerIndex); + } + child->setIcon(0, keyIcon); + ++dividerIndex; + } else { + child = childAt(parent, childIndex); + } + + QVariant value = settings->value(key); + if (value.type() == QVariant::Invalid) { + child->setText(1, "Invalid"); + } else { + child->setText(1, value.typeName()); + } + child->setText(2, VariantDelegate::displayText(value)); + child->setData(2, Qt::UserRole, value); + } + + while (dividerIndex < childCount(parent)) + delete childAt(parent, dividerIndex); +} + +QTreeWidgetItem *SettingsTree::createItem(const QString &text, + QTreeWidgetItem *parent, int index) +{ + QTreeWidgetItem *after = 0; + if (index != 0) + after = childAt(parent, index - 1); + + QTreeWidgetItem *item; + if (parent) + item = new QTreeWidgetItem(parent, after); + else + item = new QTreeWidgetItem(this, after); + + item->setText(0, text); + item->setFlags(item->flags() | Qt::ItemIsEditable); + return item; +} + +QTreeWidgetItem *SettingsTree::childAt(QTreeWidgetItem *parent, int index) +{ + if (parent) + return parent->child(index); + else + return topLevelItem(index); +} + +int SettingsTree::childCount(QTreeWidgetItem *parent) +{ + if (parent) + return parent->childCount(); + else + return topLevelItemCount(); +} + +int SettingsTree::findChild(QTreeWidgetItem *parent, const QString &text, + int startIndex) +{ + for (int i = startIndex; i < childCount(parent); ++i) { + if (childAt(parent, i)->text(0) == text) + return i; + } + return -1; +} + +void SettingsTree::moveItemForward(QTreeWidgetItem *parent, int oldIndex, + int newIndex) +{ + for (int i = 0; i < oldIndex - newIndex; ++i) + delete childAt(parent, newIndex); +} diff --git a/examples/widgets/tools/settingseditor/settingstree.h b/examples/widgets/tools/settingseditor/settingstree.h new file mode 100644 index 0000000000..60086c3c38 --- /dev/null +++ b/examples/widgets/tools/settingseditor/settingstree.h @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SETTINGSTREE_H +#define SETTINGSTREE_H + +#include <QIcon> +#include <QTimer> +#include <QTreeWidget> + +QT_BEGIN_NAMESPACE +class QSettings; +QT_END_NAMESPACE + +class SettingsTree : public QTreeWidget +{ + Q_OBJECT + +public: + SettingsTree(QWidget *parent = 0); + + void setSettingsObject(QSettings *settings); + QSize sizeHint() const; + +public slots: + void setAutoRefresh(bool autoRefresh); + void setFallbacksEnabled(bool enabled); + void maybeRefresh(); + void refresh(); + +protected: + bool event(QEvent *event); + +private slots: + void updateSetting(QTreeWidgetItem *item); + +private: + void updateChildItems(QTreeWidgetItem *parent); + QTreeWidgetItem *createItem(const QString &text, QTreeWidgetItem *parent, + int index); + QTreeWidgetItem *childAt(QTreeWidgetItem *parent, int index); + int childCount(QTreeWidgetItem *parent); + int findChild(QTreeWidgetItem *parent, const QString &text, int startIndex); + void moveItemForward(QTreeWidgetItem *parent, int oldIndex, int newIndex); + + QSettings *settings; + QTimer refreshTimer; + bool autoRefresh; + QIcon groupIcon; + QIcon keyIcon; +}; + +#endif diff --git a/examples/widgets/tools/settingseditor/variantdelegate.cpp b/examples/widgets/tools/settingseditor/variantdelegate.cpp new file mode 100644 index 0000000000..7b1da5a863 --- /dev/null +++ b/examples/widgets/tools/settingseditor/variantdelegate.cpp @@ -0,0 +1,316 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include "variantdelegate.h" + +VariantDelegate::VariantDelegate(QObject *parent) + : QItemDelegate(parent) +{ + boolExp.setPattern("true|false"); + boolExp.setCaseSensitivity(Qt::CaseInsensitive); + + byteArrayExp.setPattern("[\\x00-\\xff]*"); + charExp.setPattern("."); + colorExp.setPattern("\\(([0-9]*),([0-9]*),([0-9]*),([0-9]*)\\)"); + doubleExp.setPattern(""); + pointExp.setPattern("\\((-?[0-9]*),(-?[0-9]*)\\)"); + rectExp.setPattern("\\((-?[0-9]*),(-?[0-9]*),(-?[0-9]*),(-?[0-9]*)\\)"); + signedIntegerExp.setPattern("-?[0-9]*"); + sizeExp = pointExp; + unsignedIntegerExp.setPattern("[0-9]*"); + + dateExp.setPattern("([0-9]{,4})-([0-9]{,2})-([0-9]{,2})"); + timeExp.setPattern("([0-9]{,2}):([0-9]{,2}):([0-9]{,2})"); + dateTimeExp.setPattern(dateExp.pattern() + "T" + timeExp.pattern()); +} + +void VariantDelegate::paint(QPainter *painter, + const QStyleOptionViewItem &option, + const QModelIndex &index) const +{ + if (index.column() == 2) { + QVariant value = index.model()->data(index, Qt::UserRole); + if (!isSupportedType(value.type())) { + QStyleOptionViewItem myOption = option; + myOption.state &= ~QStyle::State_Enabled; + QItemDelegate::paint(painter, myOption, index); + return; + } + } + + QItemDelegate::paint(painter, option, index); +} + +QWidget *VariantDelegate::createEditor(QWidget *parent, + const QStyleOptionViewItem & /* option */, + const QModelIndex &index) const +{ + if (index.column() != 2) + return 0; + + QVariant originalValue = index.model()->data(index, Qt::UserRole); + if (!isSupportedType(originalValue.type())) + return 0; + + QLineEdit *lineEdit = new QLineEdit(parent); + lineEdit->setFrame(false); + + QRegExp regExp; + + switch (originalValue.type()) { + case QVariant::Bool: + regExp = boolExp; + break; + case QVariant::ByteArray: + regExp = byteArrayExp; + break; + case QVariant::Char: + regExp = charExp; + break; + case QVariant::Color: + regExp = colorExp; + break; + case QVariant::Date: + regExp = dateExp; + break; + case QVariant::DateTime: + regExp = dateTimeExp; + break; + case QVariant::Double: + regExp = doubleExp; + break; + case QVariant::Int: + case QVariant::LongLong: + regExp = signedIntegerExp; + break; + case QVariant::Point: + regExp = pointExp; + break; + case QVariant::Rect: + regExp = rectExp; + break; + case QVariant::Size: + regExp = sizeExp; + break; + case QVariant::Time: + regExp = timeExp; + break; + case QVariant::UInt: + case QVariant::ULongLong: + regExp = unsignedIntegerExp; + break; + default: + ; + } + + if (!regExp.isEmpty()) { + QValidator *validator = new QRegExpValidator(regExp, lineEdit); + lineEdit->setValidator(validator); + } + + return lineEdit; +} + +void VariantDelegate::setEditorData(QWidget *editor, + const QModelIndex &index) const +{ + QVariant value = index.model()->data(index, Qt::UserRole); + if (QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor)) + lineEdit->setText(displayText(value)); +} + +void VariantDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, + const QModelIndex &index) const +{ + QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor); + if (!lineEdit->isModified()) + return; + + QString text = lineEdit->text(); + const QValidator *validator = lineEdit->validator(); + if (validator) { + int pos; + if (validator->validate(text, pos) != QValidator::Acceptable) + return; + } + + QVariant originalValue = index.model()->data(index, Qt::UserRole); + QVariant value; + + switch (originalValue.type()) { + case QVariant::Char: + value = text.at(0); + break; + case QVariant::Color: + colorExp.exactMatch(text); + value = QColor(qMin(colorExp.cap(1).toInt(), 255), + qMin(colorExp.cap(2).toInt(), 255), + qMin(colorExp.cap(3).toInt(), 255), + qMin(colorExp.cap(4).toInt(), 255)); + break; + case QVariant::Date: + { + QDate date = QDate::fromString(text, Qt::ISODate); + if (!date.isValid()) + return; + value = date; + } + break; + case QVariant::DateTime: + { + QDateTime dateTime = QDateTime::fromString(text, Qt::ISODate); + if (!dateTime.isValid()) + return; + value = dateTime; + } + break; + case QVariant::Point: + pointExp.exactMatch(text); + value = QPoint(pointExp.cap(1).toInt(), pointExp.cap(2).toInt()); + break; + case QVariant::Rect: + rectExp.exactMatch(text); + value = QRect(rectExp.cap(1).toInt(), rectExp.cap(2).toInt(), + rectExp.cap(3).toInt(), rectExp.cap(4).toInt()); + break; + case QVariant::Size: + sizeExp.exactMatch(text); + value = QSize(sizeExp.cap(1).toInt(), sizeExp.cap(2).toInt()); + break; + case QVariant::StringList: + value = text.split(","); + break; + case QVariant::Time: + { + QTime time = QTime::fromString(text, Qt::ISODate); + if (!time.isValid()) + return; + value = time; + } + break; + default: + value = text; + value.convert(originalValue.type()); + } + + model->setData(index, displayText(value), Qt::DisplayRole); + model->setData(index, value, Qt::UserRole); +} + +bool VariantDelegate::isSupportedType(QVariant::Type type) +{ + switch (type) { + case QVariant::Bool: + case QVariant::ByteArray: + case QVariant::Char: + case QVariant::Color: + case QVariant::Date: + case QVariant::DateTime: + case QVariant::Double: + case QVariant::Int: + case QVariant::LongLong: + case QVariant::Point: + case QVariant::Rect: + case QVariant::Size: + case QVariant::String: + case QVariant::StringList: + case QVariant::Time: + case QVariant::UInt: + case QVariant::ULongLong: + return true; + default: + return false; + } +} + +QString VariantDelegate::displayText(const QVariant &value) +{ + switch (value.type()) { + case QVariant::Bool: + case QVariant::ByteArray: + case QVariant::Char: + case QVariant::Double: + case QVariant::Int: + case QVariant::LongLong: + case QVariant::String: + case QVariant::UInt: + case QVariant::ULongLong: + return value.toString(); + case QVariant::Color: + { + QColor color = qvariant_cast<QColor>(value); + return QString("(%1,%2,%3,%4)") + .arg(color.red()).arg(color.green()) + .arg(color.blue()).arg(color.alpha()); + } + case QVariant::Date: + return value.toDate().toString(Qt::ISODate); + case QVariant::DateTime: + return value.toDateTime().toString(Qt::ISODate); + case QVariant::Invalid: + return "<Invalid>"; + case QVariant::Point: + { + QPoint point = value.toPoint(); + return QString("(%1,%2)").arg(point.x()).arg(point.y()); + } + case QVariant::Rect: + { + QRect rect = value.toRect(); + return QString("(%1,%2,%3,%4)") + .arg(rect.x()).arg(rect.y()) + .arg(rect.width()).arg(rect.height()); + } + case QVariant::Size: + { + QSize size = value.toSize(); + return QString("(%1,%2)").arg(size.width()).arg(size.height()); + } + case QVariant::StringList: + return value.toStringList().join(','); + case QVariant::Time: + return value.toTime().toString(Qt::ISODate); + default: + break; + } + return QString("<%1>").arg(value.typeName()); +} diff --git a/examples/widgets/tools/settingseditor/variantdelegate.h b/examples/widgets/tools/settingseditor/variantdelegate.h new file mode 100644 index 0000000000..a886f7938a --- /dev/null +++ b/examples/widgets/tools/settingseditor/variantdelegate.h @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef VARIANTDELEGATE_H +#define VARIANTDELEGATE_H + +#include <QItemDelegate> +#include <QRegExp> + +class VariantDelegate : public QItemDelegate +{ + Q_OBJECT + +public: + VariantDelegate(QObject *parent = 0); + + void paint(QPainter *painter, const QStyleOptionViewItem &option, + const QModelIndex &index) const; + QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, + const QModelIndex &index) const; + void setEditorData(QWidget *editor, const QModelIndex &index) const; + void setModelData(QWidget *editor, QAbstractItemModel *model, + const QModelIndex &index) const; + + static bool isSupportedType(QVariant::Type type); + static QString displayText(const QVariant &value); + +private: + mutable QRegExp boolExp; + mutable QRegExp byteArrayExp; + mutable QRegExp charExp; + mutable QRegExp colorExp; + mutable QRegExp dateExp; + mutable QRegExp dateTimeExp; + mutable QRegExp doubleExp; + mutable QRegExp pointExp; + mutable QRegExp rectExp; + mutable QRegExp signedIntegerExp; + mutable QRegExp sizeExp; + mutable QRegExp timeExp; + mutable QRegExp unsignedIntegerExp; +}; + +#endif diff --git a/examples/widgets/tools/styleplugin/plugin/plugin.pro b/examples/widgets/tools/styleplugin/plugin/plugin.pro new file mode 100644 index 0000000000..c13af5a36c --- /dev/null +++ b/examples/widgets/tools/styleplugin/plugin/plugin.pro @@ -0,0 +1,25 @@ +#! [0] +TEMPLATE = lib +CONFIG += plugin +HEADERS = simplestyle.h \ + simplestyleplugin.h +SOURCES = simplestyle.cpp \ + simplestyleplugin.cpp +OTHER_FILES += simplestyle.json +TARGET = simplestyleplugin +#! [0] +win32 { + CONFIG(debug, release|debug):DESTDIR = ../debug/styles/ + CONFIG(release, release|debug):DESTDIR = ../release/styles/ +} else { + DESTDIR = ../styles/ +} + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tools/styleplugin/styles +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS plugin.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/tools/styleplugin/plugin +INSTALLS += target sources + + +QT += widgets diff --git a/examples/widgets/tools/styleplugin/plugin/simplestyle.cpp b/examples/widgets/tools/styleplugin/plugin/simplestyle.cpp new file mode 100644 index 0000000000..7f47a36335 --- /dev/null +++ b/examples/widgets/tools/styleplugin/plugin/simplestyle.cpp @@ -0,0 +1,48 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include "simplestyle.h" + +void SimpleStyle::polish(QPalette &palette) +{ + palette.setBrush(QPalette::Button, Qt::red); +} diff --git a/examples/widgets/tools/styleplugin/plugin/simplestyle.h b/examples/widgets/tools/styleplugin/plugin/simplestyle.h new file mode 100644 index 0000000000..15d5fb3b7f --- /dev/null +++ b/examples/widgets/tools/styleplugin/plugin/simplestyle.h @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SIMPLESTYLE_H +#define SIMPLESTYLE_H + +#include <QProxyStyle> + +QT_BEGIN_NAMESPACE +class QPalette; +QT_END_NAMESPACE + +class SimpleStyle : public QProxyStyle +{ + Q_OBJECT + +public: + SimpleStyle() {}; + + void polish(QPalette &palette); +}; + +#endif diff --git a/examples/widgets/tools/styleplugin/plugin/simplestyle.json b/examples/widgets/tools/styleplugin/plugin/simplestyle.json new file mode 100644 index 0000000000..a708e2aafe --- /dev/null +++ b/examples/widgets/tools/styleplugin/plugin/simplestyle.json @@ -0,0 +1,3 @@ +{ + "Keys": [ "simplestyle" ] +} diff --git a/examples/widgets/tools/styleplugin/plugin/simplestyleplugin.cpp b/examples/widgets/tools/styleplugin/plugin/simplestyleplugin.cpp new file mode 100644 index 0000000000..08c2a8720c --- /dev/null +++ b/examples/widgets/tools/styleplugin/plugin/simplestyleplugin.cpp @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include "simplestyleplugin.h" +#include "simplestyle.h" + +//! [0] +QStringList SimpleStylePlugin::keys() const +{ + return QStringList() << "SimpleStyle"; +} +//! [0] + +//! [1] +QStyle *SimpleStylePlugin::create(const QString &key) +{ + if (key.toLower() == "simplestyle") + return new SimpleStyle; + return 0; +} +//! [1] diff --git a/examples/widgets/tools/styleplugin/plugin/simplestyleplugin.h b/examples/widgets/tools/styleplugin/plugin/simplestyleplugin.h new file mode 100644 index 0000000000..ac585dd80a --- /dev/null +++ b/examples/widgets/tools/styleplugin/plugin/simplestyleplugin.h @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SIMPLESTYLEPLUGIN_H +#define SIMPLESTYLEPLUGIN_H + +#include <QStylePlugin> + +QT_BEGIN_NAMESPACE +class QStringList; +class QStyle; +QT_END_NAMESPACE + +//! [0] +class SimpleStylePlugin : public QStylePlugin +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QStyleFactoryInterface" FILE "simplestyle.json") + +public: + SimpleStylePlugin() {} + + QStringList keys() const; + QStyle *create(const QString &key); +}; +//! [0] + +#endif diff --git a/examples/widgets/tools/styleplugin/styleplugin.pro b/examples/widgets/tools/styleplugin/styleplugin.pro new file mode 100644 index 0000000000..63805a14cc --- /dev/null +++ b/examples/widgets/tools/styleplugin/styleplugin.pro @@ -0,0 +1,11 @@ +TEMPLATE = subdirs +SUBDIRS = stylewindow \ + plugin + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tools/styleplugin +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS styleplugin.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/tools/styleplugin +INSTALLS += target sources + +QT += widgets diff --git a/examples/widgets/tools/styleplugin/stylewindow/main.cpp b/examples/widgets/tools/styleplugin/stylewindow/main.cpp new file mode 100644 index 0000000000..5f791e0774 --- /dev/null +++ b/examples/widgets/tools/styleplugin/stylewindow/main.cpp @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include "stylewindow.h" + +//! [0] +int main(int argv, char *args[]) +{ + QApplication app(argv, args); + QApplication::setStyle(QStyleFactory::create("simplestyle")); + + StyleWindow window; + window.resize(200, 50); + window.show(); + + return app.exec(); +} +//! [0] diff --git a/examples/widgets/tools/styleplugin/stylewindow/stylewindow.cpp b/examples/widgets/tools/styleplugin/stylewindow/stylewindow.cpp new file mode 100644 index 0000000000..83d6634152 --- /dev/null +++ b/examples/widgets/tools/styleplugin/stylewindow/stylewindow.cpp @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include "stylewindow.h" + +StyleWindow::StyleWindow() +{ + QPushButton *styledButton = new QPushButton(tr("Big Red Button")); + + QGridLayout *layout = new QGridLayout; + layout->addWidget(styledButton); + + QGroupBox *styleBox = new QGroupBox(tr("A simple style button")); + styleBox->setLayout(layout); + + QGridLayout *outerLayout = new QGridLayout; + outerLayout->addWidget(styleBox, 0, 0); + setLayout(outerLayout); + + setWindowTitle(tr("Style Plugin Example")); +} diff --git a/examples/widgets/tools/styleplugin/stylewindow/stylewindow.h b/examples/widgets/tools/styleplugin/stylewindow/stylewindow.h new file mode 100644 index 0000000000..fa8cdbadd2 --- /dev/null +++ b/examples/widgets/tools/styleplugin/stylewindow/stylewindow.h @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef STYLEWINDOW_H +#define STYLEWINDOW_H + +#include <QWidget> + +class StyleWindow : public QWidget +{ + Q_OBJECT + +public: + StyleWindow(); +}; + +#endif diff --git a/examples/widgets/tools/styleplugin/stylewindow/stylewindow.pro b/examples/widgets/tools/styleplugin/stylewindow/stylewindow.pro new file mode 100644 index 0000000000..658a46134b --- /dev/null +++ b/examples/widgets/tools/styleplugin/stylewindow/stylewindow.pro @@ -0,0 +1,19 @@ +HEADERS = stylewindow.h +SOURCES = stylewindow.cpp \ + main.cpp + +TARGET = styleplugin +win32 { + debug:DESTDIR = ../debug/ + release:DESTDIR = ../release/ +} else { + DESTDIR = ../ +} + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tools/styleplugin +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS stylewindow.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/tools/styleplugin/stylewindow +INSTALLS += target sources + +QT += widgets diff --git a/examples/widgets/tools/tools.pro b/examples/widgets/tools/tools.pro new file mode 100644 index 0000000000..aaf4440d23 --- /dev/null +++ b/examples/widgets/tools/tools.pro @@ -0,0 +1,22 @@ +TEMPLATE = subdirs +SUBDIRS = \ + codecs \ + completer \ + customcompleter \ + echoplugin \ + i18n \ + plugandpaint \ + plugandpaintplugins \ + regexp \ + settingseditor \ + styleplugin \ + treemodelcompleter \ + undo \ + undoframework + +plugandpaint.depends = plugandpaintplugins + +# install +sources.files = tools.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/widgets/tools +INSTALLS += sources diff --git a/examples/widgets/tools/treemodelcompleter/main.cpp b/examples/widgets/tools/treemodelcompleter/main.cpp new file mode 100644 index 0000000000..65990ca54c --- /dev/null +++ b/examples/widgets/tools/treemodelcompleter/main.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> +#include "mainwindow.h" + +//! [0] +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(treemodelcompleter); + + QApplication app(argc, argv); + MainWindow window; + window.show(); + return app.exec(); +} +//! [0] diff --git a/examples/widgets/tools/treemodelcompleter/mainwindow.cpp b/examples/widgets/tools/treemodelcompleter/mainwindow.cpp new file mode 100644 index 0000000000..851fde5fff --- /dev/null +++ b/examples/widgets/tools/treemodelcompleter/mainwindow.cpp @@ -0,0 +1,246 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> +#include "treemodelcompleter.h" +#include "mainwindow.h" + +//! [0] +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent), completer(0), lineEdit(0) +{ + createMenu(); + + completer = new TreeModelCompleter(this); + completer->setModel(modelFromFile(":/resources/treemodel.txt")); + completer->setSeparator(QLatin1String(".")); + QObject::connect(completer, SIGNAL(highlighted(QModelIndex)), + this, SLOT(highlight(QModelIndex))); + + QWidget *centralWidget = new QWidget; + + QLabel *modelLabel = new QLabel; + modelLabel->setText(tr("Tree Model<br>(Double click items to edit)")); + + QLabel *modeLabel = new QLabel; + modeLabel->setText(tr("Completion Mode")); + modeCombo = new QComboBox; + modeCombo->addItem(tr("Inline")); + modeCombo->addItem(tr("Filtered Popup")); + modeCombo->addItem(tr("Unfiltered Popup")); + modeCombo->setCurrentIndex(1); + + QLabel *caseLabel = new QLabel; + caseLabel->setText(tr("Case Sensitivity")); + caseCombo = new QComboBox; + caseCombo->addItem(tr("Case Insensitive")); + caseCombo->addItem(tr("Case Sensitive")); + caseCombo->setCurrentIndex(0); +//! [0] + +//! [1] + QLabel *separatorLabel = new QLabel; + separatorLabel->setText(tr("Tree Separator")); + + QLineEdit *separatorLineEdit = new QLineEdit; + separatorLineEdit->setText(completer->separator()); + connect(separatorLineEdit, SIGNAL(textChanged(QString)), + completer, SLOT(setSeparator(QString))); + + QCheckBox *wrapCheckBox = new QCheckBox; + wrapCheckBox->setText(tr("Wrap around completions")); + wrapCheckBox->setChecked(completer->wrapAround()); + connect(wrapCheckBox, SIGNAL(clicked(bool)), completer, SLOT(setWrapAround(bool))); + + contentsLabel = new QLabel; + contentsLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); + connect(separatorLineEdit, SIGNAL(textChanged(QString)), + this, SLOT(updateContentsLabel(QString))); + + treeView = new QTreeView; + treeView->setModel(completer->model()); + treeView->header()->hide(); + treeView->expandAll(); +//! [1] + +//! [2] + connect(modeCombo, SIGNAL(activated(int)), this, SLOT(changeMode(int))); + connect(caseCombo, SIGNAL(activated(int)), this, SLOT(changeCase(int))); + + lineEdit = new QLineEdit; + lineEdit->setCompleter(completer); +//! [2] + +//! [3] + QGridLayout *layout = new QGridLayout; + layout->addWidget(modelLabel, 0, 0); layout->addWidget(treeView, 0, 1); + layout->addWidget(modeLabel, 1, 0); layout->addWidget(modeCombo, 1, 1); + layout->addWidget(caseLabel, 2, 0); layout->addWidget(caseCombo, 2, 1); + layout->addWidget(separatorLabel, 3, 0); layout->addWidget(separatorLineEdit, 3, 1); + layout->addWidget(wrapCheckBox, 4, 0); + layout->addWidget(contentsLabel, 5, 0, 1, 2); + layout->addWidget(lineEdit, 6, 0, 1, 2); + centralWidget->setLayout(layout); + setCentralWidget(centralWidget); + + changeCase(caseCombo->currentIndex()); + changeMode(modeCombo->currentIndex()); + + setWindowTitle(tr("Tree Model Completer")); + lineEdit->setFocus(); +} +//! [3] + +//! [4] +void MainWindow::createMenu() +{ + QAction *exitAction = new QAction(tr("Exit"), this); + QAction *aboutAct = new QAction(tr("About"), this); + QAction *aboutQtAct = new QAction(tr("About Qt"), this); + + connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit())); + connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); + connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); + + QMenu* fileMenu = menuBar()->addMenu(tr("File")); + fileMenu->addAction(exitAction); + + QMenu* helpMenu = menuBar()->addMenu(tr("About")); + helpMenu->addAction(aboutAct); + helpMenu->addAction(aboutQtAct); +} +//! [4] + +//! [5] +void MainWindow::changeMode(int index) +{ + QCompleter::CompletionMode mode; + if (index == 0) + mode = QCompleter::InlineCompletion; + else if (index == 1) + mode = QCompleter::PopupCompletion; + else + mode = QCompleter::UnfilteredPopupCompletion; + + completer->setCompletionMode(mode); +} +//! [5] + +QAbstractItemModel *MainWindow::modelFromFile(const QString& fileName) +{ + QFile file(fileName); + if (!file.open(QFile::ReadOnly)) + return new QStringListModel(completer); + +#ifndef QT_NO_CURSOR + QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); +#endif + QStringList words; + + QStandardItemModel *model = new QStandardItemModel(completer); + QVector<QStandardItem *> parents(10); + parents[0] = model->invisibleRootItem(); + + while (!file.atEnd()) { + QString line = file.readLine(); + QString trimmedLine = line.trimmed(); + if (line.isEmpty() || trimmedLine.isEmpty()) + continue; + + QRegExp re("^\\s+"); + int nonws = re.indexIn(line); + int level = 0; + if (nonws == -1) { + level = 0; + } else { + if (line.startsWith("\t")) { + level = re.cap(0).length(); + } else { + level = re.cap(0).length()/4; + } + } + + if (level+1 >= parents.size()) + parents.resize(parents.size()*2); + + QStandardItem *item = new QStandardItem; + item->setText(trimmedLine); + parents[level]->appendRow(item); + parents[level+1] = item; + } + +#ifndef QT_NO_CURSOR + QApplication::restoreOverrideCursor(); +#endif + + return model; +} + +void MainWindow::highlight(const QModelIndex &index) +{ + QAbstractItemModel *completionModel = completer->completionModel(); + QAbstractProxyModel *proxy = qobject_cast<QAbstractProxyModel *>(completionModel); + if (!proxy) + return; + QModelIndex sourceIndex = proxy->mapToSource(index); + treeView->selectionModel()->select(sourceIndex, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows); + treeView->scrollTo(index); +} + +//! [6] +void MainWindow::about() +{ + QMessageBox::about(this, tr("About"), tr("This example demonstrates how " + "to use a QCompleter with a custom tree model.")); +} +//! [6] + +//! [7] +void MainWindow::changeCase(int cs) +{ + completer->setCaseSensitivity(cs ? Qt::CaseSensitive : Qt::CaseInsensitive); +} +//! [7] + +void MainWindow::updateContentsLabel(const QString& sep) +{ + contentsLabel->setText(tr("Type path from model above with items at each level separated by a '%1'").arg(sep)); +} + diff --git a/examples/widgets/tools/treemodelcompleter/mainwindow.h b/examples/widgets/tools/treemodelcompleter/mainwindow.h new file mode 100644 index 0000000000..206a41c2fc --- /dev/null +++ b/examples/widgets/tools/treemodelcompleter/mainwindow.h @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> +#include <QModelIndex> + +class TreeModelCompleter; +QT_BEGIN_NAMESPACE +class QAbstractItemModel; +class QComboBox; +class QLabel; +class QLineEdit; +class QProgressBar; +class QCheckBox; +class QTreeView; +QT_END_NAMESPACE + +//! [0] +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = 0); + +private slots: + void about(); + void changeCase(int); + void changeMode(int); + void highlight(const QModelIndex&); + void updateContentsLabel(const QString&); +//! [0] + +//! [1] +private: + void createMenu(); + QAbstractItemModel *modelFromFile(const QString& fileName); + + QTreeView *treeView; + QComboBox *caseCombo; + QComboBox *modeCombo; + QLabel *contentsLabel; + TreeModelCompleter *completer; + QLineEdit *lineEdit; +}; +//! [1] + +#endif // MAINWINDOW_H diff --git a/examples/widgets/tools/treemodelcompleter/resources/treemodel.txt b/examples/widgets/tools/treemodelcompleter/resources/treemodel.txt new file mode 100644 index 0000000000..06c90077af --- /dev/null +++ b/examples/widgets/tools/treemodelcompleter/resources/treemodel.txt @@ -0,0 +1,20 @@ +Parent1 + Child1 + GrandChild1 + GrandChild2 + GrandChild3 + GrandGrandChild1 + Child2 + GrandChild1 + GrandGrandChild1 + GrandChild2 + Child3 + +Parent2 + Child1 + GrandChild1 + Child2 + Child3 + GrandChild1 + GrandChild2 + diff --git a/examples/widgets/tools/treemodelcompleter/treemodelcompleter.cpp b/examples/widgets/tools/treemodelcompleter/treemodelcompleter.cpp new file mode 100644 index 0000000000..71adaac077 --- /dev/null +++ b/examples/widgets/tools/treemodelcompleter/treemodelcompleter.cpp @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "treemodelcompleter.h" +#include <QStringList> + +//! [0] +TreeModelCompleter::TreeModelCompleter(QObject *parent) + : QCompleter(parent) +{ +} +//! [0] + +//! [1] +TreeModelCompleter::TreeModelCompleter(QAbstractItemModel *model, QObject *parent) + : QCompleter(model, parent) +{ +} +//! [1] + +void TreeModelCompleter::setSeparator(const QString &separator) +{ + sep = separator; +} + +//! [2] +QString TreeModelCompleter::separator() const +{ + return sep; +} +//! [2] + +//! [3] +QStringList TreeModelCompleter::splitPath(const QString &path) const +{ + if (sep.isNull()) { + return QCompleter::splitPath(path); + } + + return path.split(sep); +} +//! [3] + +//! [4] +QString TreeModelCompleter::pathFromIndex(const QModelIndex &index) const +{ + if (sep.isNull()) { + return QCompleter::pathFromIndex(index); + } + + // navigate up and accumulate data + QStringList dataList; + for (QModelIndex i = index; i.isValid(); i = i.parent()) { + dataList.prepend(model()->data(i, completionRole()).toString()); + } + + return dataList.join(sep); +} +//! [4] + diff --git a/examples/widgets/tools/treemodelcompleter/treemodelcompleter.desktop b/examples/widgets/tools/treemodelcompleter/treemodelcompleter.desktop new file mode 100644 index 0000000000..a54aa7b66e --- /dev/null +++ b/examples/widgets/tools/treemodelcompleter/treemodelcompleter.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=Tree Model Completer +Exec=/opt/usr/bin/treemodelcompleter +Icon=treemodelcompleter +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tools/treemodelcompleter/treemodelcompleter.h b/examples/widgets/tools/treemodelcompleter/treemodelcompleter.h new file mode 100644 index 0000000000..f0288805d3 --- /dev/null +++ b/examples/widgets/tools/treemodelcompleter/treemodelcompleter.h @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TREEMODELCOMPLETER_H +#define TREEMODELCOMPLETER_H + +#include <QCompleter> + +//! [0] +class TreeModelCompleter : public QCompleter +{ + Q_OBJECT + Q_PROPERTY(QString separator READ separator WRITE setSeparator) + +public: + explicit TreeModelCompleter(QObject *parent = 0); + explicit TreeModelCompleter(QAbstractItemModel *model, QObject *parent = 0); + + QString separator() const; +public slots: + void setSeparator(const QString &separator); + +protected: + QStringList splitPath(const QString &path) const; + QString pathFromIndex(const QModelIndex &index) const; + +private: + QString sep; +}; +//! [0] + +#endif // TREEMODELCOMPLETER_H + diff --git a/examples/widgets/tools/treemodelcompleter/treemodelcompleter.pro b/examples/widgets/tools/treemodelcompleter/treemodelcompleter.pro new file mode 100644 index 0000000000..ffdf9b647d --- /dev/null +++ b/examples/widgets/tools/treemodelcompleter/treemodelcompleter.pro @@ -0,0 +1,16 @@ +HEADERS = treemodelcompleter.h \ + mainwindow.h +SOURCES = treemodelcompleter.cpp \ + main.cpp \ + mainwindow.cpp +RESOURCES = treemodelcompleter.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tools/treemodelcompleter +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS treemodelcompleter.pro resources +sources.path = $$[QT_INSTALL_EXAMPLES]/tools/treemodelcompleter +INSTALLS += target sources + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tools/treemodelcompleter/treemodelcompleter.qrc b/examples/widgets/tools/treemodelcompleter/treemodelcompleter.qrc new file mode 100644 index 0000000000..1130fcd641 --- /dev/null +++ b/examples/widgets/tools/treemodelcompleter/treemodelcompleter.qrc @@ -0,0 +1,5 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix="/"> + <file>resources/treemodel.txt</file> +</qresource> +</RCC> diff --git a/examples/widgets/tools/undo/commands.cpp b/examples/widgets/tools/undo/commands.cpp new file mode 100644 index 0000000000..ee299bc3af --- /dev/null +++ b/examples/widgets/tools/undo/commands.cpp @@ -0,0 +1,180 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "commands.h" + +static const int setShapeRectCommandId = 1; +static const int setShapeColorCommandId = 2; + +/****************************************************************************** +** AddShapeCommand +*/ + +AddShapeCommand::AddShapeCommand(Document *doc, const Shape &shape, QUndoCommand *parent) + : QUndoCommand(parent) +{ + m_doc = doc; + m_shape = shape; +} + +void AddShapeCommand::undo() +{ + m_doc->deleteShape(m_shapeName); +} + +void AddShapeCommand::redo() +{ + // A shape only gets a name when it is inserted into a document + m_shapeName = m_doc->addShape(m_shape); + setText(QObject::tr("Add %1").arg(m_shapeName)); +} + +/****************************************************************************** +** RemoveShapeCommand +*/ + +RemoveShapeCommand::RemoveShapeCommand(Document *doc, const QString &shapeName, + QUndoCommand *parent) + : QUndoCommand(parent) +{ + setText(QObject::tr("Remove %1").arg(shapeName)); + m_doc = doc; + m_shape = doc->shape(shapeName); + m_shapeName = shapeName; +} + +void RemoveShapeCommand::undo() +{ + m_shapeName = m_doc->addShape(m_shape); +} + +void RemoveShapeCommand::redo() +{ + m_doc->deleteShape(m_shapeName); +} + +/****************************************************************************** +** SetShapeColorCommand +*/ + +SetShapeColorCommand::SetShapeColorCommand(Document *doc, const QString &shapeName, + const QColor &color, QUndoCommand *parent) + : QUndoCommand(parent) +{ + setText(QObject::tr("Set %1's color").arg(shapeName)); + + m_doc = doc; + m_shapeName = shapeName; + m_oldColor = doc->shape(shapeName).color(); + m_newColor = color; +} + +void SetShapeColorCommand::undo() +{ + m_doc->setShapeColor(m_shapeName, m_oldColor); +} + +void SetShapeColorCommand::redo() +{ + m_doc->setShapeColor(m_shapeName, m_newColor); +} + +bool SetShapeColorCommand::mergeWith(const QUndoCommand *command) +{ + if (command->id() != setShapeColorCommandId) + return false; + + const SetShapeColorCommand *other = static_cast<const SetShapeColorCommand*>(command); + if (m_shapeName != other->m_shapeName) + return false; + + m_newColor = other->m_newColor; + return true; +} + +int SetShapeColorCommand::id() const +{ + return setShapeColorCommandId; +} + +/****************************************************************************** +** SetShapeRectCommand +*/ + +SetShapeRectCommand::SetShapeRectCommand(Document *doc, const QString &shapeName, + const QRect &rect, QUndoCommand *parent) + : QUndoCommand(parent) +{ + setText(QObject::tr("Change %1's geometry").arg(shapeName)); + + m_doc = doc; + m_shapeName = shapeName; + m_oldRect = doc->shape(shapeName).rect(); + m_newRect = rect; +} + +void SetShapeRectCommand::undo() +{ + m_doc->setShapeRect(m_shapeName, m_oldRect); +} + +void SetShapeRectCommand::redo() +{ + m_doc->setShapeRect(m_shapeName, m_newRect); +} + +bool SetShapeRectCommand::mergeWith(const QUndoCommand *command) +{ + if (command->id() != setShapeRectCommandId) + return false; + + const SetShapeRectCommand *other = static_cast<const SetShapeRectCommand*>(command); + if (m_shapeName != other->m_shapeName) + return false; + + m_newRect = other->m_newRect; + return true; +} + +int SetShapeRectCommand::id() const +{ + return setShapeRectCommandId; +} diff --git a/examples/widgets/tools/undo/commands.h b/examples/widgets/tools/undo/commands.h new file mode 100644 index 0000000000..23dde1c81c --- /dev/null +++ b/examples/widgets/tools/undo/commands.h @@ -0,0 +1,112 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef COMMANDS_H +#define COMMANDS_H + +#include <QUndoCommand> +#include "document.h" + +class AddShapeCommand : public QUndoCommand +{ +public: + AddShapeCommand(Document *doc, const Shape &shape, QUndoCommand *parent = 0); + void undo(); + void redo(); + +private: + Document *m_doc; + Shape m_shape; + QString m_shapeName; +}; + +class RemoveShapeCommand : public QUndoCommand +{ +public: + RemoveShapeCommand(Document *doc, const QString &shapeName, QUndoCommand *parent = 0); + void undo(); + void redo(); + +private: + Document *m_doc; + Shape m_shape; + QString m_shapeName; +}; + +class SetShapeColorCommand : public QUndoCommand +{ +public: + SetShapeColorCommand(Document *doc, const QString &shapeName, const QColor &color, + QUndoCommand *parent = 0); + + void undo(); + void redo(); + + bool mergeWith(const QUndoCommand *command); + int id() const; + +private: + Document *m_doc; + QString m_shapeName; + QColor m_oldColor; + QColor m_newColor; +}; + +class SetShapeRectCommand : public QUndoCommand +{ +public: + SetShapeRectCommand(Document *doc, const QString &shapeName, const QRect &rect, + QUndoCommand *parent = 0); + + void undo(); + void redo(); + + bool mergeWith(const QUndoCommand *command); + int id() const; + +private: + Document *m_doc; + QString m_shapeName; + QRect m_oldRect; + QRect m_newRect; +}; + +#endif // COMMANDS_H diff --git a/examples/widgets/tools/undo/document.cpp b/examples/widgets/tools/undo/document.cpp new file mode 100644 index 0000000000..5970ab72a1 --- /dev/null +++ b/examples/widgets/tools/undo/document.cpp @@ -0,0 +1,445 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <qevent.h> +#include <QPainter> +#include <QTextStream> +#include <QUndoStack> +#include "document.h" +#include "commands.h" + +static const int resizeHandleWidth = 6; + +/****************************************************************************** +** Shape +*/ + +const QSize Shape::minSize(80, 50); + +Shape::Shape(Type type, const QColor &color, const QRect &rect) + : m_type(type), m_rect(rect), m_color(color) +{ +} + +Shape::Type Shape::type() const +{ + return m_type; +} + +QRect Shape::rect() const +{ + return m_rect; +} + +QColor Shape::color() const +{ + return m_color; +} + +QString Shape::name() const +{ + return m_name; +} + +QRect Shape::resizeHandle() const +{ + QPoint br = m_rect.bottomRight(); + return QRect(br - QPoint(resizeHandleWidth, resizeHandleWidth), br); +} + +QString Shape::typeToString(Type type) +{ + QString result; + + switch (type) { + case Rectangle: + result = QLatin1String("Rectangle"); + break; + case Circle: + result = QLatin1String("Circle"); + break; + case Triangle: + result = QLatin1String("Triangle"); + break; + } + + return result; +} + +Shape::Type Shape::stringToType(const QString &s, bool *ok) +{ + if (ok != 0) + *ok = true; + + if (s == QLatin1String("Rectangle")) + return Rectangle; + if (s == QLatin1String("Circle")) + return Circle; + if (s == QLatin1String("Triangle")) + return Triangle; + + if (ok != 0) + *ok = false; + return Rectangle; +} + +/****************************************************************************** +** Document +*/ + +Document::Document(QWidget *parent) + : QWidget(parent), m_currentIndex(-1), m_mousePressIndex(-1), m_resizeHandlePressed(false) +{ + m_undoStack = new QUndoStack(this); + + setAutoFillBackground(true); + setBackgroundRole(QPalette::Base); + + QPalette pal = palette(); + pal.setBrush(QPalette::Base, QPixmap(":/icons/background.png")); + pal.setColor(QPalette::HighlightedText, Qt::red); + setPalette(pal); +} + +QString Document::addShape(const Shape &shape) +{ + QString name = Shape::typeToString(shape.type()); + name = uniqueName(name); + + m_shapeList.append(shape); + m_shapeList[m_shapeList.count() - 1].m_name = name; + setCurrentShape(m_shapeList.count() - 1); + + return name; +} + +void Document::deleteShape(const QString &shapeName) +{ + int index = indexOf(shapeName); + if (index == -1) + return; + + update(m_shapeList.at(index).rect()); + + m_shapeList.removeAt(index); + + if (index <= m_currentIndex) { + m_currentIndex = -1; + if (index == m_shapeList.count()) + --index; + setCurrentShape(index); + } +} + +Shape Document::shape(const QString &shapeName) const +{ + int index = indexOf(shapeName); + if (index == -1) + return Shape(); + return m_shapeList.at(index); +} + +void Document::setShapeRect(const QString &shapeName, const QRect &rect) +{ + int index = indexOf(shapeName); + if (index == -1) + return; + + Shape &shape = m_shapeList[index]; + + update(shape.rect()); + update(rect); + + shape.m_rect = rect; +} + +void Document::setShapeColor(const QString &shapeName, const QColor &color) +{ + + int index = indexOf(shapeName); + if (index == -1) + return; + + Shape &shape = m_shapeList[index]; + shape.m_color = color; + + update(shape.rect()); +} + +QUndoStack *Document::undoStack() const +{ + return m_undoStack; +} + +bool Document::load(QTextStream &stream) +{ + m_shapeList.clear(); + + while (!stream.atEnd()) { + QString shapeType, shapeName, colorName; + int left, top, width, height; + stream >> shapeType >> shapeName >> colorName >> left >> top >> width >> height; + if (stream.status() != QTextStream::Ok) + return false; + bool ok; + Shape::Type type = Shape::stringToType(shapeType, &ok); + if (!ok) + return false; + QColor color(colorName); + if (!color.isValid()) + return false; + + Shape shape(type); + shape.m_name = shapeName; + shape.m_color = color; + shape.m_rect = QRect(left, top, width, height); + + m_shapeList.append(shape); + } + + m_currentIndex = m_shapeList.isEmpty() ? -1 : 0; + + return true; +} + +void Document::save(QTextStream &stream) +{ + for (int i = 0; i < m_shapeList.count(); ++i) { + const Shape &shape = m_shapeList.at(i); + QRect r = shape.rect(); + stream << Shape::typeToString(shape.type()) << QLatin1Char(' ') + << shape.name() << QLatin1Char(' ') + << shape.color().name() << QLatin1Char(' ') + << r.left() << QLatin1Char(' ') + << r.top() << QLatin1Char(' ') + << r.width() << QLatin1Char(' ') + << r.height(); + if (i != m_shapeList.count() - 1) + stream << QLatin1Char('\n'); + } + m_undoStack->setClean(); +} + +QString Document::fileName() const +{ + return m_fileName; +} + +void Document::setFileName(const QString &fileName) +{ + m_fileName = fileName; +} + +int Document::indexAt(const QPoint &pos) const +{ + for (int i = m_shapeList.count() - 1; i >= 0; --i) { + if (m_shapeList.at(i).rect().contains(pos)) + return i; + } + return -1; +} + +void Document::mousePressEvent(QMouseEvent *event) +{ + event->accept(); + int index = indexAt(event->pos());; + if (index != -1) { + setCurrentShape(index); + + const Shape &shape = m_shapeList.at(index); + m_resizeHandlePressed = shape.resizeHandle().contains(event->pos()); + + if (m_resizeHandlePressed) + m_mousePressOffset = shape.rect().bottomRight() - event->pos(); + else + m_mousePressOffset = event->pos() - shape.rect().topLeft(); + } + m_mousePressIndex = index; +} + +void Document::mouseReleaseEvent(QMouseEvent *event) +{ + event->accept(); + m_mousePressIndex = -1; +} + +void Document::mouseMoveEvent(QMouseEvent *event) +{ + event->accept(); + + if (m_mousePressIndex == -1) + return; + + const Shape &shape = m_shapeList.at(m_mousePressIndex); + + QRect rect; + if (m_resizeHandlePressed) { + rect = QRect(shape.rect().topLeft(), event->pos() + m_mousePressOffset); + } else { + rect = shape.rect(); + rect.moveTopLeft(event->pos() - m_mousePressOffset); + } + + QSize size = rect.size().expandedTo(Shape::minSize); + rect.setSize(size); + + m_undoStack->push(new SetShapeRectCommand(this, shape.name(), rect)); +} + +static QGradient gradient(const QColor &color, const QRect &rect) +{ + QColor c = color; + c.setAlpha(160); + QLinearGradient result(rect.topLeft(), rect.bottomRight()); + result.setColorAt(0, c.dark(150)); + result.setColorAt(0.5, c.light(200)); + result.setColorAt(1, c.dark(150)); + return result; +} + +static QPolygon triangle(const QRect &rect) +{ + QPolygon result(3); + result.setPoint(0, rect.center().x(), rect.top()); + result.setPoint(1, rect.right(), rect.bottom()); + result.setPoint(2, rect.left(), rect.bottom()); + return result; +} + +void Document::paintEvent(QPaintEvent *event) +{ + QRegion paintRegion = event->region(); + QPainter painter(this); + QPalette pal = palette(); + + for (int i = 0; i < m_shapeList.count(); ++i) { + const Shape &shape = m_shapeList.at(i); + + if (!paintRegion.contains(shape.rect())) + continue; + + QPen pen = pal.text().color(); + pen.setWidth(i == m_currentIndex ? 2 : 1); + painter.setPen(pen); + painter.setBrush(gradient(shape.color(), shape.rect())); + + QRect rect = shape.rect(); + rect.adjust(1, 1, -resizeHandleWidth/2, -resizeHandleWidth/2); + + // paint the shape + switch (shape.type()) { + case Shape::Rectangle: + painter.drawRect(rect); + break; + case Shape::Circle: + painter.setRenderHint(QPainter::Antialiasing); + painter.drawEllipse(rect); + painter.setRenderHint(QPainter::Antialiasing, false); + break; + case Shape::Triangle: + painter.setRenderHint(QPainter::Antialiasing); + painter.drawPolygon(triangle(rect)); + painter.setRenderHint(QPainter::Antialiasing, false); + break; + } + + // paint the resize handle + painter.setPen(pal.text().color()); + painter.setBrush(Qt::white); + painter.drawRect(shape.resizeHandle().adjusted(0, 0, -1, -1)); + + // paint the shape name + painter.setBrush(pal.text()); + if (shape.type() == Shape::Triangle) + rect.adjust(0, rect.height()/2, 0, 0); + painter.drawText(rect, Qt::AlignCenter, shape.name()); + } +} + +void Document::setCurrentShape(int index) +{ + QString currentName; + + if (m_currentIndex != -1) + update(m_shapeList.at(m_currentIndex).rect()); + + m_currentIndex = index; + + if (m_currentIndex != -1) { + const Shape ¤t = m_shapeList.at(m_currentIndex); + update(current.rect()); + currentName = current.name(); + } + + emit currentShapeChanged(currentName); +} + +int Document::indexOf(const QString &shapeName) const +{ + for (int i = 0; i < m_shapeList.count(); ++i) { + if (m_shapeList.at(i).name() == shapeName) + return i; + } + return -1; +} + +QString Document::uniqueName(const QString &name) const +{ + QString unique; + + for (int i = 0; ; ++i) { + unique = name; + if (i > 0) + unique += QString::number(i); + if (indexOf(unique) == -1) + break; + } + + return unique; +} + +QString Document::currentShapeName() const +{ + if (m_currentIndex == -1) + return QString(); + return m_shapeList.at(m_currentIndex).name(); +} + diff --git a/examples/widgets/tools/undo/document.h b/examples/widgets/tools/undo/document.h new file mode 100644 index 0000000000..1c18e02aa1 --- /dev/null +++ b/examples/widgets/tools/undo/document.h @@ -0,0 +1,125 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DOCUMENT_H +#define DOCUMENT_H + +#include <QWidget> + +QT_FORWARD_DECLARE_CLASS(QUndoStack) +QT_FORWARD_DECLARE_CLASS(QTextStream) + +class Shape +{ +public: + enum Type { Rectangle, Circle, Triangle }; + + explicit Shape(Type type = Rectangle, const QColor &color = Qt::red, const QRect &rect = QRect()); + + Type type() const; + QString name() const; + QRect rect() const; + QRect resizeHandle() const; + QColor color() const; + + static QString typeToString(Type type); + static Type stringToType(const QString &s, bool *ok = 0); + + static const QSize minSize; + +private: + Type m_type; + QRect m_rect; + QColor m_color; + QString m_name; + + friend class Document; +}; + +class Document : public QWidget +{ + Q_OBJECT + +public: + Document(QWidget *parent = 0); + + QString addShape(const Shape &shape); + void deleteShape(const QString &shapeName); + Shape shape(const QString &shapeName) const; + QString currentShapeName() const; + + void setShapeRect(const QString &shapeName, const QRect &rect); + void setShapeColor(const QString &shapeName, const QColor &color); + + bool load(QTextStream &stream); + void save(QTextStream &stream); + + QString fileName() const; + void setFileName(const QString &fileName); + + QUndoStack *undoStack() const; + +signals: + void currentShapeChanged(const QString &shapeName); + +protected: + void paintEvent(QPaintEvent *event); + void mousePressEvent(QMouseEvent *event); + void mouseReleaseEvent(QMouseEvent *event); + void mouseMoveEvent(QMouseEvent *event); + +private: + void setCurrentShape(int index); + int indexOf(const QString &shapeName) const; + int indexAt(const QPoint &pos) const; + QString uniqueName(const QString &name) const; + + QList<Shape> m_shapeList; + int m_currentIndex; + int m_mousePressIndex; + QPoint m_mousePressOffset; + bool m_resizeHandlePressed; + QString m_fileName; + + QUndoStack *m_undoStack; +}; + +#endif // DOCUMENT_H diff --git a/examples/widgets/tools/undo/icons/background.png b/examples/widgets/tools/undo/icons/background.png Binary files differnew file mode 100644 index 0000000000..3bc5ed8cf0 --- /dev/null +++ b/examples/widgets/tools/undo/icons/background.png diff --git a/examples/widgets/tools/undo/icons/blue.png b/examples/widgets/tools/undo/icons/blue.png Binary files differnew file mode 100644 index 0000000000..4e181bb61a --- /dev/null +++ b/examples/widgets/tools/undo/icons/blue.png diff --git a/examples/widgets/tools/undo/icons/circle.png b/examples/widgets/tools/undo/icons/circle.png Binary files differnew file mode 100644 index 0000000000..ed16c6e144 --- /dev/null +++ b/examples/widgets/tools/undo/icons/circle.png diff --git a/examples/widgets/tools/undo/icons/exit.png b/examples/widgets/tools/undo/icons/exit.png Binary files differnew file mode 100644 index 0000000000..539cb2ead9 --- /dev/null +++ b/examples/widgets/tools/undo/icons/exit.png diff --git a/examples/widgets/tools/undo/icons/fileclose.png b/examples/widgets/tools/undo/icons/fileclose.png Binary files differnew file mode 100644 index 0000000000..c5483d14ab --- /dev/null +++ b/examples/widgets/tools/undo/icons/fileclose.png diff --git a/examples/widgets/tools/undo/icons/filenew.png b/examples/widgets/tools/undo/icons/filenew.png Binary files differnew file mode 100644 index 0000000000..57e57e343b --- /dev/null +++ b/examples/widgets/tools/undo/icons/filenew.png diff --git a/examples/widgets/tools/undo/icons/fileopen.png b/examples/widgets/tools/undo/icons/fileopen.png Binary files differnew file mode 100644 index 0000000000..33e0d6394c --- /dev/null +++ b/examples/widgets/tools/undo/icons/fileopen.png diff --git a/examples/widgets/tools/undo/icons/filesave.png b/examples/widgets/tools/undo/icons/filesave.png Binary files differnew file mode 100644 index 0000000000..57fd5e2f34 --- /dev/null +++ b/examples/widgets/tools/undo/icons/filesave.png diff --git a/examples/widgets/tools/undo/icons/green.png b/examples/widgets/tools/undo/icons/green.png Binary files differnew file mode 100644 index 0000000000..e2e7cc9e50 --- /dev/null +++ b/examples/widgets/tools/undo/icons/green.png diff --git a/examples/widgets/tools/undo/icons/ok.png b/examples/widgets/tools/undo/icons/ok.png Binary files differnew file mode 100644 index 0000000000..e355ea91bc --- /dev/null +++ b/examples/widgets/tools/undo/icons/ok.png diff --git a/examples/widgets/tools/undo/icons/rectangle.png b/examples/widgets/tools/undo/icons/rectangle.png Binary files differnew file mode 100644 index 0000000000..3a7d9795fd --- /dev/null +++ b/examples/widgets/tools/undo/icons/rectangle.png diff --git a/examples/widgets/tools/undo/icons/red.png b/examples/widgets/tools/undo/icons/red.png Binary files differnew file mode 100644 index 0000000000..58c3e7253b --- /dev/null +++ b/examples/widgets/tools/undo/icons/red.png diff --git a/examples/widgets/tools/undo/icons/redo.png b/examples/widgets/tools/undo/icons/redo.png Binary files differnew file mode 100644 index 0000000000..5591517e1c --- /dev/null +++ b/examples/widgets/tools/undo/icons/redo.png diff --git a/examples/widgets/tools/undo/icons/remove.png b/examples/widgets/tools/undo/icons/remove.png Binary files differnew file mode 100644 index 0000000000..7a7b048c0a --- /dev/null +++ b/examples/widgets/tools/undo/icons/remove.png diff --git a/examples/widgets/tools/undo/icons/triangle.png b/examples/widgets/tools/undo/icons/triangle.png Binary files differnew file mode 100644 index 0000000000..2969131c31 --- /dev/null +++ b/examples/widgets/tools/undo/icons/triangle.png diff --git a/examples/widgets/tools/undo/icons/undo.png b/examples/widgets/tools/undo/icons/undo.png Binary files differnew file mode 100644 index 0000000000..8cf63a8ec9 --- /dev/null +++ b/examples/widgets/tools/undo/icons/undo.png diff --git a/examples/widgets/tools/undo/main.cpp b/examples/widgets/tools/undo/main.cpp new file mode 100644 index 0000000000..b62e1aca75 --- /dev/null +++ b/examples/widgets/tools/undo/main.cpp @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> +#include "mainwindow.h" + +int main(int argc, char **argv) +{ + Q_INIT_RESOURCE(undo); + + QApplication app(argc, argv); + + MainWindow win; + win.resize(800, 600); + win.show(); + + return app.exec(); +}; diff --git a/examples/widgets/tools/undo/mainwindow.cpp b/examples/widgets/tools/undo/mainwindow.cpp new file mode 100644 index 0000000000..22cea1dbbf --- /dev/null +++ b/examples/widgets/tools/undo/mainwindow.cpp @@ -0,0 +1,446 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QUndoGroup> +#include <QUndoStack> +#include <QFileDialog> +#include <QMessageBox> +#include <QTextStream> +#include <QToolButton> +#include "document.h" +#include "mainwindow.h" +#include "commands.h" + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) +{ + setupUi(this); + + QWidget *w = documentTabs->widget(0); + documentTabs->removeTab(0); + delete w; + + connect(actionOpen, SIGNAL(triggered()), this, SLOT(openDocument())); + connect(actionClose, SIGNAL(triggered()), this, SLOT(closeDocument())); + connect(actionNew, SIGNAL(triggered()), this, SLOT(newDocument())); + connect(actionSave, SIGNAL(triggered()), this, SLOT(saveDocument())); + connect(actionExit, SIGNAL(triggered()), this, SLOT(close())); + connect(actionRed, SIGNAL(triggered()), this, SLOT(setShapeColor())); + connect(actionGreen, SIGNAL(triggered()), this, SLOT(setShapeColor())); + connect(actionBlue, SIGNAL(triggered()), this, SLOT(setShapeColor())); + connect(actionAddCircle, SIGNAL(triggered()), this, SLOT(addShape())); + connect(actionAddRectangle, SIGNAL(triggered()), this, SLOT(addShape())); + connect(actionAddTriangle, SIGNAL(triggered()), this, SLOT(addShape())); + connect(actionRemoveShape, SIGNAL(triggered()), this, SLOT(removeShape())); + connect(actionAddRobot, SIGNAL(triggered()), this, SLOT(addRobot())); + connect(actionAddSnowman, SIGNAL(triggered()), this, SLOT(addSnowman())); + connect(actionAbout, SIGNAL(triggered()), this, SLOT(about())); + connect(actionAboutQt, SIGNAL(triggered()), this, SLOT(aboutQt())); + + connect(undoLimit, SIGNAL(valueChanged(int)), this, SLOT(updateActions())); + connect(documentTabs, SIGNAL(currentChanged(int)), this, SLOT(updateActions())); + + actionOpen->setShortcut(QString("Ctrl+O")); + actionClose->setShortcut(QString("Ctrl+W")); + actionNew->setShortcut(QString("Ctrl+N")); + actionSave->setShortcut(QString("Ctrl+S")); + actionExit->setShortcut(QString("Ctrl+Q")); + actionRemoveShape->setShortcut(QString("Del")); + actionRed->setShortcut(QString("Alt+R")); + actionGreen->setShortcut(QString("Alt+G")); + actionBlue->setShortcut(QString("Alt+B")); + actionAddCircle->setShortcut(QString("Alt+C")); + actionAddRectangle->setShortcut(QString("Alt+L")); + actionAddTriangle->setShortcut(QString("Alt+T")); + + m_undoGroup = new QUndoGroup(this); + undoView->setGroup(m_undoGroup); + undoView->setCleanIcon(QIcon(":/icons/ok.png")); + + QAction *undoAction = m_undoGroup->createUndoAction(this); + QAction *redoAction = m_undoGroup->createRedoAction(this); + undoAction->setIcon(QIcon(":/icons/undo.png")); + redoAction->setIcon(QIcon(":/icons/redo.png")); + menuShape->insertAction(menuShape->actions().at(0), undoAction); + menuShape->insertAction(undoAction, redoAction); + + toolBar->addAction(undoAction); + toolBar->addAction(redoAction); + + newDocument(); + updateActions(); +}; + +void MainWindow::updateActions() +{ + Document *doc = currentDocument(); + m_undoGroup->setActiveStack(doc == 0 ? 0 : doc->undoStack()); + QString shapeName = doc == 0 ? QString() : doc->currentShapeName(); + + actionAddRobot->setEnabled(doc != 0); + actionAddSnowman->setEnabled(doc != 0); + actionAddCircle->setEnabled(doc != 0); + actionAddRectangle->setEnabled(doc != 0); + actionAddTriangle->setEnabled(doc != 0); + actionClose->setEnabled(doc != 0); + actionSave->setEnabled(doc != 0 && !doc->undoStack()->isClean()); + undoLimit->setEnabled(doc != 0 && doc->undoStack()->count() == 0); + + if (shapeName.isEmpty()) { + actionRed->setEnabled(false); + actionGreen->setEnabled(false); + actionBlue->setEnabled(false); + actionRemoveShape->setEnabled(false); + } else { + Shape shape = doc->shape(shapeName); + actionRed->setEnabled(shape.color() != Qt::red); + actionGreen->setEnabled(shape.color() != Qt::green); + actionBlue->setEnabled(shape.color() != Qt::blue); + actionRemoveShape->setEnabled(true); + } + + if (doc != 0) { + int index = documentTabs->indexOf(doc); + Q_ASSERT(index != -1); + static const QIcon unsavedIcon(":/icons/filesave.png"); + documentTabs->setTabIcon(index, doc->undoStack()->isClean() ? QIcon() : unsavedIcon); + + if (doc->undoStack()->count() == 0) + doc->undoStack()->setUndoLimit(undoLimit->value()); + } +} + +void MainWindow::openDocument() +{ + QString fileName = QFileDialog::getOpenFileName(this); + if (fileName.isEmpty()) + return; + + QFile file(fileName); + if (!file.open(QIODevice::ReadOnly)) { + QMessageBox::warning(this, + tr("File error"), + tr("Failed to open\n%1").arg(fileName)); + return; + } + QTextStream stream(&file); + + Document *doc = new Document(); + if (!doc->load(stream)) { + QMessageBox::warning(this, + tr("Parse error"), + tr("Failed to parse\n%1").arg(fileName)); + delete doc; + return; + } + + doc->setFileName(fileName); + addDocument(doc); +} + +QString MainWindow::fixedWindowTitle(const Document *doc) const +{ + QString title = doc->fileName(); + + if (title.isEmpty()) + title = tr("Unnamed"); + else + title = QFileInfo(title).fileName(); + + QString result; + + for (int i = 0; ; ++i) { + result = title; + if (i > 0) + result += QString::number(i); + + bool unique = true; + for (int j = 0; j < documentTabs->count(); ++j) { + const QWidget *widget = documentTabs->widget(j); + if (widget == doc) + continue; + if (result == documentTabs->tabText(j)) { + unique = false; + break; + } + } + + if (unique) + break; + } + + return result; +} + +void MainWindow::addDocument(Document *doc) +{ + if (documentTabs->indexOf(doc) != -1) + return; + m_undoGroup->addStack(doc->undoStack()); + documentTabs->addTab(doc, fixedWindowTitle(doc)); + connect(doc, SIGNAL(currentShapeChanged(QString)), this, SLOT(updateActions())); + connect(doc->undoStack(), SIGNAL(indexChanged(int)), this, SLOT(updateActions())); + connect(doc->undoStack(), SIGNAL(cleanChanged(bool)), this, SLOT(updateActions())); + + setCurrentDocument(doc); +} + +void MainWindow::setCurrentDocument(Document *doc) +{ + documentTabs->setCurrentWidget(doc); +} + +Document *MainWindow::currentDocument() const +{ + return qobject_cast<Document*>(documentTabs->currentWidget()); +} + +void MainWindow::removeDocument(Document *doc) +{ + int index = documentTabs->indexOf(doc); + if (index == -1) + return; + + documentTabs->removeTab(index); + m_undoGroup->removeStack(doc->undoStack()); + disconnect(doc, SIGNAL(currentShapeChanged(QString)), this, SLOT(updateActions())); + disconnect(doc->undoStack(), SIGNAL(indexChanged(int)), this, SLOT(updateActions())); + disconnect(doc->undoStack(), SIGNAL(cleanChanged(bool)), this, SLOT(updateActions())); + + if (documentTabs->count() == 0) { + newDocument(); + updateActions(); + } +} + +void MainWindow::saveDocument() +{ + Document *doc = currentDocument(); + if (doc == 0) + return; + + for (;;) { + QString fileName = doc->fileName(); + + if (fileName.isEmpty()) + fileName = QFileDialog::getSaveFileName(this); + if (fileName.isEmpty()) + break; + + QFile file(fileName); + if (!file.open(QIODevice::WriteOnly)) { + QMessageBox::warning(this, + tr("File error"), + tr("Failed to open\n%1").arg(fileName)); + doc->setFileName(QString()); + } else { + QTextStream stream(&file); + doc->save(stream); + doc->setFileName(fileName); + + int index = documentTabs->indexOf(doc); + Q_ASSERT(index != -1); + documentTabs->setTabText(index, fixedWindowTitle(doc)); + + break; + } + } +} + +void MainWindow::closeDocument() +{ + Document *doc = currentDocument(); + if (doc == 0) + return; + + if (!doc->undoStack()->isClean()) { + int button + = QMessageBox::warning(this, + tr("Unsaved changes"), + tr("Would you like to save this document?"), + QMessageBox::Yes, QMessageBox::No); + if (button == QMessageBox::Yes) + saveDocument(); + } + + removeDocument(doc); + delete doc; +} + +void MainWindow::newDocument() +{ + addDocument(new Document()); +} + +static QColor randomColor() +{ + int r = (int) (3.0*(rand()/(RAND_MAX + 1.0))); + switch (r) { + case 0: + return Qt::red; + case 1: + return Qt::green; + default: + break; + } + return Qt::blue; +} + +static QRect randomRect(const QSize &s) +{ + QSize min = Shape::minSize; + + int left = (int) ((0.0 + s.width() - min.width())*(rand()/(RAND_MAX + 1.0))); + int top = (int) ((0.0 + s.height() - min.height())*(rand()/(RAND_MAX + 1.0))); + int width = (int) ((0.0 + s.width() - left - min.width())*(rand()/(RAND_MAX + 1.0))) + min.width(); + int height = (int) ((0.0 + s.height() - top - min.height())*(rand()/(RAND_MAX + 1.0))) + min.height(); + + return QRect(left, top, width, height); +} + +void MainWindow::addShape() +{ + Document *doc = currentDocument(); + if (doc == 0) + return; + + Shape::Type type; + + if (sender() == actionAddCircle) + type = Shape::Circle; + else if (sender() == actionAddRectangle) + type = Shape::Rectangle; + else if (sender() == actionAddTriangle) + type = Shape::Triangle; + else return; + + Shape newShape(type, randomColor(), randomRect(doc->size())); + doc->undoStack()->push(new AddShapeCommand(doc, newShape)); +} + +void MainWindow::removeShape() +{ + Document *doc = currentDocument(); + if (doc == 0) + return; + + QString shapeName = doc->currentShapeName(); + if (shapeName.isEmpty()) + return; + + doc->undoStack()->push(new RemoveShapeCommand(doc, shapeName)); +} + +void MainWindow::setShapeColor() +{ + Document *doc = currentDocument(); + if (doc == 0) + return; + + QString shapeName = doc->currentShapeName(); + if (shapeName.isEmpty()) + return; + + QColor color; + + if (sender() == actionRed) + color = Qt::red; + else if (sender() == actionGreen) + color = Qt::green; + else if (sender() == actionBlue) + color = Qt::blue; + else + return; + + if (color == doc->shape(shapeName).color()) + return; + + doc->undoStack()->push(new SetShapeColorCommand(doc, shapeName, color)); +} + +void MainWindow::addSnowman() +{ + Document *doc = currentDocument(); + if (doc == 0) + return; + + // Create a macro command using beginMacro() and endMacro() + + doc->undoStack()->beginMacro(tr("Add snowman")); + doc->undoStack()->push(new AddShapeCommand(doc, + Shape(Shape::Circle, Qt::blue, QRect(51, 30, 97, 95)))); + doc->undoStack()->push(new AddShapeCommand(doc, + Shape(Shape::Circle, Qt::blue, QRect(27, 123, 150, 133)))); + doc->undoStack()->push(new AddShapeCommand(doc, + Shape(Shape::Circle, Qt::blue, QRect(11, 253, 188, 146)))); + doc->undoStack()->endMacro(); +} + +void MainWindow::addRobot() +{ + Document *doc = currentDocument(); + if (doc == 0) + return; + + // Compose a macro command by explicitly adding children to a parent command + + QUndoCommand *parent = new QUndoCommand(tr("Add robot")); + + new AddShapeCommand(doc, Shape(Shape::Rectangle, Qt::green, QRect(115, 15, 81, 70)), parent); + new AddShapeCommand(doc, Shape(Shape::Rectangle, Qt::green, QRect(82, 89, 148, 188)), parent); + new AddShapeCommand(doc, Shape(Shape::Rectangle, Qt::green, QRect(76, 280, 80, 165)), parent); + new AddShapeCommand(doc, Shape(Shape::Rectangle, Qt::green, QRect(163, 280, 80, 164)), parent); + new AddShapeCommand(doc, Shape(Shape::Circle, Qt::blue, QRect(116, 25, 80, 50)), parent); + new AddShapeCommand(doc, Shape(Shape::Rectangle, Qt::green, QRect(232, 92, 80, 127)), parent); + new AddShapeCommand(doc, Shape(Shape::Rectangle, Qt::green, QRect(2, 92, 80, 125)), parent); + + doc->undoStack()->push(parent); +} + +void MainWindow::about() +{ + QMessageBox::about(this, tr("About Undo"), tr("The Undo demonstration shows how to use the Qt Undo framework.")); +} + +void MainWindow::aboutQt() +{ + QMessageBox::aboutQt(this, tr("About Qt")); +} diff --git a/examples/widgets/tools/undo/mainwindow.h b/examples/widgets/tools/undo/mainwindow.h new file mode 100644 index 0000000000..3218c889e4 --- /dev/null +++ b/examples/widgets/tools/undo/mainwindow.h @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> +#include "ui_mainwindow.h" + +class Document; + +class MainWindow : public QMainWindow, public Ui::MainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = 0); + + void addDocument(Document *doc); + void removeDocument(Document *doc); + void setCurrentDocument(Document *doc); + Document *currentDocument() const; + +public slots: + void openDocument(); + void saveDocument(); + void closeDocument(); + void newDocument(); + + void addShape(); + void removeShape(); + void setShapeColor(); + + void addSnowman(); + void addRobot(); + + void about(); + void aboutQt(); + +private slots: + void updateActions(); + +private: + QUndoGroup *m_undoGroup; + + QString fixedWindowTitle(const Document *doc) const; +}; + +#endif // MAINWINDOW_H diff --git a/examples/widgets/tools/undo/mainwindow.ui b/examples/widgets/tools/undo/mainwindow.ui new file mode 100644 index 0000000000..91a0b437e5 --- /dev/null +++ b/examples/widgets/tools/undo/mainwindow.ui @@ -0,0 +1,322 @@ +<ui version="4.0" > + <class>MainWindow</class> + <widget class="QMainWindow" name="MainWindow" > + <property name="geometry" > + <rect> + <x>0</x> + <y>0</y> + <width>567</width> + <height>600</height> + </rect> + </property> + <property name="iconSize" > + <size> + <width>32</width> + <height>32</height> + </size> + </property> + <widget class="QWidget" name="centralwidget" > + <layout class="QVBoxLayout" > + <property name="margin" > + <number>0</number> + </property> + <property name="spacing" > + <number>0</number> + </property> + <item> + <widget class="QTabWidget" name="documentTabs" > + <property name="currentIndex" > + <number>0</number> + </property> + <widget class="QWidget" name="tab" > + <attribute name="title" > + <string>Tab 1</string> + </attribute> + </widget> + </widget> + </item> + </layout> + </widget> + <widget class="QMenuBar" name="menubar" > + <property name="geometry" > + <rect> + <x>0</x> + <y>0</y> + <width>567</width> + <height>27</height> + </rect> + </property> + <widget class="QMenu" name="menuFile" > + <property name="title" > + <string>File</string> + </property> + <addaction name="actionNew" /> + <addaction name="actionOpen" /> + <addaction name="actionSave" /> + <addaction name="actionClose" /> + <addaction name="separator" /> + <addaction name="actionExit" /> + </widget> + <widget class="QMenu" name="menuShape" > + <property name="title" > + <string>Edit</string> + </property> + <widget class="QMenu" name="menuMacros" > + <property name="title" > + <string>Macros</string> + </property> + <addaction name="actionAddRobot" /> + <addaction name="actionAddSnowman" /> + </widget> + <addaction name="separator" /> + <addaction name="actionAddCircle" /> + <addaction name="actionAddRectangle" /> + <addaction name="actionAddTriangle" /> + <addaction name="actionRemoveShape" /> + <addaction name="separator" /> + <addaction name="actionRed" /> + <addaction name="actionGreen" /> + <addaction name="actionBlue" /> + <addaction name="separator" /> + <addaction name="menuMacros" /> + </widget> + <widget class="QMenu" name="menuHelp" > + <property name="title" > + <string>Help</string> + </property> + <addaction name="actionAbout" /> + <addaction name="actionAboutQt" /> + </widget> + <addaction name="menuFile" /> + <addaction name="menuShape" /> + <addaction name="menuHelp" /> + </widget> + <widget class="QStatusBar" name="statusbar" /> + <widget class="QToolBar" name="toolBar" > + <property name="windowTitle" > + <string>File actions</string> + </property> + <property name="orientation" > + <enum>Qt::Horizontal</enum> + </property> + <attribute name="toolBarArea" > + <enum>TopToolBarArea</enum> + </attribute> + <attribute name="toolBarBreak" > + <bool>false</bool> + </attribute> + <addaction name="actionNew" /> + <addaction name="actionOpen" /> + <addaction name="actionSave" /> + <addaction name="actionClose" /> + <addaction name="separator" /> + </widget> + <widget class="QToolBar" name="shapeToolBar" > + <property name="windowTitle" > + <string>Shape actions</string> + </property> + <property name="orientation" > + <enum>Qt::Vertical</enum> + </property> + <attribute name="toolBarArea" > + <enum>LeftToolBarArea</enum> + </attribute> + <attribute name="toolBarBreak" > + <bool>false</bool> + </attribute> + <addaction name="actionAddRectangle" /> + <addaction name="actionAddCircle" /> + <addaction name="actionAddTriangle" /> + <addaction name="actionRemoveShape" /> + <addaction name="separator" /> + <addaction name="actionRed" /> + <addaction name="actionGreen" /> + <addaction name="actionBlue" /> + </widget> + <widget class="QDockWidget" name="dockWidget" > + <property name="windowTitle" > + <string>Undo Stack</string> + </property> + <attribute name="dockWidgetArea" > + <number>2</number> + </attribute> + <widget class="QWidget" name="dockWidgetContents" > + <layout class="QVBoxLayout" > + <property name="margin" > + <number>0</number> + </property> + <property name="spacing" > + <number>4</number> + </property> + <item> + <layout class="QHBoxLayout" > + <property name="margin" > + <number>0</number> + </property> + <property name="spacing" > + <number>6</number> + </property> + <item> + <spacer> + <property name="orientation" > + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" > + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QLabel" name="label" > + <property name="text" > + <string>Undo limit</string> + </property> + </widget> + </item> + <item> + <widget class="QSpinBox" name="undoLimit" /> + </item> + </layout> + </item> + <item> + <widget class="QUndoView" name="undoView" > + <property name="alternatingRowColors" > + <bool>false</bool> + </property> + </widget> + </item> + </layout> + </widget> + </widget> + <action name="actionOpen" > + <property name="icon" > + <iconset resource="undo.qrc" >:/icons/fileopen.png</iconset> + </property> + <property name="text" > + <string>&Open</string> + </property> + </action> + <action name="actionClose" > + <property name="icon" > + <iconset resource="undo.qrc" >:/icons/fileclose.png</iconset> + </property> + <property name="text" > + <string>&Close</string> + </property> + </action> + <action name="actionNew" > + <property name="icon" > + <iconset resource="undo.qrc" >:/icons/filenew.png</iconset> + </property> + <property name="text" > + <string>&New</string> + </property> + </action> + <action name="actionSave" > + <property name="icon" > + <iconset resource="undo.qrc" >:/icons/filesave.png</iconset> + </property> + <property name="text" > + <string>&Save</string> + </property> + </action> + <action name="actionExit" > + <property name="icon" > + <iconset resource="undo.qrc" >:/icons/exit.png</iconset> + </property> + <property name="text" > + <string>E&xit</string> + </property> + </action> + <action name="actionRed" > + <property name="icon" > + <iconset resource="undo.qrc" >:/icons/red.png</iconset> + </property> + <property name="text" > + <string>Red</string> + </property> + </action> + <action name="actionGreen" > + <property name="icon" > + <iconset resource="undo.qrc" >:/icons/green.png</iconset> + </property> + <property name="text" > + <string>Green</string> + </property> + </action> + <action name="actionBlue" > + <property name="icon" > + <iconset resource="undo.qrc" >:/icons/blue.png</iconset> + </property> + <property name="text" > + <string>Blue</string> + </property> + </action> + <action name="actionAddRectangle" > + <property name="icon" > + <iconset resource="undo.qrc" >:/icons/rectangle.png</iconset> + </property> + <property name="text" > + <string>Add Rectangle</string> + </property> + </action> + <action name="actionAddCircle" > + <property name="icon" > + <iconset resource="undo.qrc" >:/icons/circle.png</iconset> + </property> + <property name="text" > + <string>Add Circle</string> + </property> + </action> + <action name="actionRemoveShape" > + <property name="icon" > + <iconset resource="undo.qrc" >:/icons/remove.png</iconset> + </property> + <property name="text" > + <string>Remove Shape</string> + </property> + </action> + <action name="actionAddRobot" > + <property name="text" > + <string>Add robot</string> + </property> + </action> + <action name="actionAddSnowman" > + <property name="text" > + <string>Add snowan</string> + </property> + </action> + <action name="actionAddTriangle" > + <property name="icon" > + <iconset resource="undo.qrc" >:/icons/triangle.png</iconset> + </property> + <property name="text" > + <string>addTriangle</string> + </property> + </action> + <action name="actionAbout" > + <property name="text" > + <string>About</string> + </property> + </action> + <action name="actionAboutQt" > + <property name="text" > + <string>About Qt</string> + </property> + </action> + </widget> + <customwidgets> + <customwidget> + <class>QUndoView</class> + <extends>QListView</extends> + <header>qundoview.h</header> + </customwidget> + </customwidgets> + <resources> + <include location="undo.qrc" /> + </resources> + <connections/> +</ui> diff --git a/examples/widgets/tools/undo/undo.pro b/examples/widgets/tools/undo/undo.pro new file mode 100644 index 0000000000..e61b678a93 --- /dev/null +++ b/examples/widgets/tools/undo/undo.pro @@ -0,0 +1,18 @@ +SOURCES += main.cpp mainwindow.cpp commands.cpp document.cpp +HEADERS += mainwindow.h commands.h document.h +FORMS += mainwindow.ui + +build_all:!build_pass { + CONFIG -= build_all + CONFIG += release +} + +RESOURCES += undo.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tools/undo +sources.files = $$SOURCES $$HEADERS *.pro icons $$RESOURCES $$FORMS +sources.path = $$[QT_INSTALL_EXAMPLES]/tools/undo +INSTALLS += target sources + +QT += widgets diff --git a/examples/widgets/tools/undo/undo.qrc b/examples/widgets/tools/undo/undo.qrc new file mode 100644 index 0000000000..65619b8f1a --- /dev/null +++ b/examples/widgets/tools/undo/undo.qrc @@ -0,0 +1,20 @@ +<RCC> + <qresource prefix="/" > + <file>icons/background.png</file> + <file>icons/blue.png</file> + <file>icons/circle.png</file> + <file>icons/exit.png</file> + <file>icons/fileclose.png</file> + <file>icons/filenew.png</file> + <file>icons/fileopen.png</file> + <file>icons/filesave.png</file> + <file>icons/green.png</file> + <file>icons/ok.png</file> + <file>icons/rectangle.png</file> + <file>icons/red.png</file> + <file>icons/redo.png</file> + <file>icons/remove.png</file> + <file>icons/triangle.png</file> + <file>icons/undo.png</file> + </qresource> +</RCC> diff --git a/examples/widgets/tools/undoframework/commands.cpp b/examples/widgets/tools/undoframework/commands.cpp new file mode 100644 index 0000000000..2a1fad95ea --- /dev/null +++ b/examples/widgets/tools/undoframework/commands.cpp @@ -0,0 +1,168 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include "commands.h" +#include "diagramitem.h" + +//! [0] +MoveCommand::MoveCommand(DiagramItem *diagramItem, const QPointF &oldPos, + QUndoCommand *parent) + : QUndoCommand(parent) +{ + myDiagramItem = diagramItem; + newPos = diagramItem->pos(); + myOldPos = oldPos; +} +//! [0] + +//! [1] +bool MoveCommand::mergeWith(const QUndoCommand *command) +{ + const MoveCommand *moveCommand = static_cast<const MoveCommand *>(command); + DiagramItem *item = moveCommand->myDiagramItem; + + if (myDiagramItem != item) + return false; + + newPos = item->pos(); + setText(QObject::tr("Move %1") + .arg(createCommandString(myDiagramItem, newPos))); + + return true; +} +//! [1] + +//! [2] +void MoveCommand::undo() +{ + myDiagramItem->setPos(myOldPos); + myDiagramItem->scene()->update(); + setText(QObject::tr("Move %1") + .arg(createCommandString(myDiagramItem, newPos))); +} +//! [2] + +//! [3] +void MoveCommand::redo() +{ + myDiagramItem->setPos(newPos); + setText(QObject::tr("Move %1") + .arg(createCommandString(myDiagramItem, newPos))); +} +//! [3] + +//! [4] +DeleteCommand::DeleteCommand(QGraphicsScene *scene, QUndoCommand *parent) + : QUndoCommand(parent) +{ + myGraphicsScene = scene; + QList<QGraphicsItem *> list = myGraphicsScene->selectedItems(); + list.first()->setSelected(false); + myDiagramItem = static_cast<DiagramItem *>(list.first()); + setText(QObject::tr("Delete %1") + .arg(createCommandString(myDiagramItem, myDiagramItem->pos()))); +} +//! [4] + +//! [5] +void DeleteCommand::undo() +{ + myGraphicsScene->addItem(myDiagramItem); + myGraphicsScene->update(); +} +//! [5] + +//! [6] +void DeleteCommand::redo() +{ + myGraphicsScene->removeItem(myDiagramItem); +} +//! [6] + +//! [7] +AddCommand::AddCommand(DiagramItem::DiagramType addType, + QGraphicsScene *scene, QUndoCommand *parent) + : QUndoCommand(parent) +{ + static int itemCount = 0; + + myGraphicsScene = scene; + myDiagramItem = new DiagramItem(addType); + initialPosition = QPointF((itemCount * 15) % int(scene->width()), + (itemCount * 15) % int(scene->height())); + scene->update(); + ++itemCount; + setText(QObject::tr("Add %1") + .arg(createCommandString(myDiagramItem, initialPosition))); +} +//! [7] + +AddCommand::~AddCommand() +{ + if (!myDiagramItem->scene()) + delete myDiagramItem; +} + +//! [8] +void AddCommand::undo() +{ + myGraphicsScene->removeItem(myDiagramItem); + myGraphicsScene->update(); +} +//! [8] + +//! [9] +void AddCommand::redo() +{ + myGraphicsScene->addItem(myDiagramItem); + myDiagramItem->setPos(initialPosition); + myGraphicsScene->clearSelection(); + myGraphicsScene->update(); +} +//! [9] + +QString createCommandString(DiagramItem *item, const QPointF &pos) +{ + return QObject::tr("%1 at (%2, %3)") + .arg(item->diagramType() == DiagramItem::Box ? "Box" : "Triangle") + .arg(pos.x()).arg(pos.y()); +} diff --git a/examples/widgets/tools/undoframework/commands.h b/examples/widgets/tools/undoframework/commands.h new file mode 100644 index 0000000000..7318668285 --- /dev/null +++ b/examples/widgets/tools/undoframework/commands.h @@ -0,0 +1,104 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef COMMANDS_H +#define COMMANDS_H + +#include <QUndoCommand> + +#include "diagramitem.h" + +//! [0] +class MoveCommand : public QUndoCommand +{ +public: + enum { Id = 1234 }; + + MoveCommand(DiagramItem *diagramItem, const QPointF &oldPos, + QUndoCommand *parent = 0); + + void undo(); + void redo(); + bool mergeWith(const QUndoCommand *command); + int id() const { return Id; } + +private: + DiagramItem *myDiagramItem; + QPointF myOldPos; + QPointF newPos; +}; +//! [0] + +//! [1] +class DeleteCommand : public QUndoCommand +{ +public: + explicit DeleteCommand(QGraphicsScene *graphicsScene, QUndoCommand *parent = 0); + + void undo(); + void redo(); + +private: + DiagramItem *myDiagramItem; + QGraphicsScene *myGraphicsScene; +}; +//! [1] + +//! [2] +class AddCommand : public QUndoCommand +{ +public: + AddCommand(DiagramItem::DiagramType addType, QGraphicsScene *graphicsScene, + QUndoCommand *parent = 0); + ~AddCommand(); + + void undo(); + void redo(); + +private: + DiagramItem *myDiagramItem; + QGraphicsScene *myGraphicsScene; + QPointF initialPosition; +}; +//! [2] + +QString createCommandString(DiagramItem *item, const QPointF &point); + +#endif diff --git a/examples/widgets/tools/undoframework/diagramitem.cpp b/examples/widgets/tools/undoframework/diagramitem.cpp new file mode 100644 index 0000000000..611069cd88 --- /dev/null +++ b/examples/widgets/tools/undoframework/diagramitem.cpp @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include "diagramitem.h" + +DiagramItem::DiagramItem(DiagramType diagramType, QGraphicsItem *item) + : QGraphicsPolygonItem(item) +{ + if (diagramType == Box) { + boxPolygon << QPointF(0, 0) << QPointF(0, 30) << QPointF(30, 30) + << QPointF(30, 0) << QPointF(0, 0); + setPolygon(boxPolygon); + } else { + trianglePolygon << QPointF(15, 0) << QPointF(30, 30) << QPointF(0, 30) + << QPointF(15, 0); + setPolygon(trianglePolygon); + } + + QColor color(static_cast<int>(qrand()) % 256, + static_cast<int>(qrand()) % 256, static_cast<int>(qrand()) % 256); + QBrush brush(color); + setBrush(brush); + setFlag(QGraphicsItem::ItemIsSelectable); + setFlag(QGraphicsItem::ItemIsMovable); +} diff --git a/examples/widgets/tools/undoframework/diagramitem.h b/examples/widgets/tools/undoframework/diagramitem.h new file mode 100644 index 0000000000..7b7fefa7f7 --- /dev/null +++ b/examples/widgets/tools/undoframework/diagramitem.h @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DIAGRAMITEM_H +#define DIAGRAMITEM_H + +#include <QGraphicsPolygonItem> + +QT_BEGIN_NAMESPACE +class QGraphicsItem; +class QGraphicsScene; +class QGraphicsSceneMouseEvent; +class QPointF; +QT_END_NAMESPACE + +class DiagramItem : public QGraphicsPolygonItem +{ +public: + enum { Type = UserType + 1 }; + enum DiagramType { Box, Triangle }; + + explicit DiagramItem(DiagramType diagramType, QGraphicsItem *item = 0); + + DiagramType diagramType() const { + return polygon() == boxPolygon ? Box : Triangle; + } + int type() const { return Type; } + +private: + QPolygonF boxPolygon; + QPolygonF trianglePolygon; +}; + +#endif diff --git a/examples/widgets/tools/undoframework/diagramscene.cpp b/examples/widgets/tools/undoframework/diagramscene.cpp new file mode 100644 index 0000000000..2c40b71219 --- /dev/null +++ b/examples/widgets/tools/undoframework/diagramscene.cpp @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include "diagramscene.h" +#include "diagramitem.h" + +DiagramScene::DiagramScene(QObject *parent) + : QGraphicsScene(parent) +{ + movingItem = 0; +} + +void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *event) +{ + QPointF mousePos(event->buttonDownScenePos(Qt::LeftButton).x(), + event->buttonDownScenePos(Qt::LeftButton).y()); + const QList<QGraphicsItem *> itemList = items(mousePos); + movingItem = itemList.isEmpty() ? 0 : itemList.first(); + + if (movingItem != 0 && event->button() == Qt::LeftButton) { + oldPos = movingItem->pos(); + } + + clearSelection(); + QGraphicsScene::mousePressEvent(event); +} + +void DiagramScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) +{ + if (movingItem != 0 && event->button() == Qt::LeftButton) { + if (oldPos != movingItem->pos()) + emit itemMoved(qgraphicsitem_cast<DiagramItem *>(movingItem), + oldPos); + movingItem = 0; + } + QGraphicsScene::mouseReleaseEvent(event); +} diff --git a/examples/widgets/tools/undoframework/diagramscene.h b/examples/widgets/tools/undoframework/diagramscene.h new file mode 100644 index 0000000000..e6150a3da0 --- /dev/null +++ b/examples/widgets/tools/undoframework/diagramscene.h @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DIAGRAMSCENE_H +#define DIAGRAMSCENE_H + +#include <QObject> +#include <QGraphicsScene> + +class DiagramItem; +QT_BEGIN_NAMESPACE +class QGraphicsSceneDragDropEvent; +class QGraphicsViewItem; +QT_END_NAMESPACE + +//! [0] +class DiagramScene : public QGraphicsScene +{ + Q_OBJECT + +public: + DiagramScene(QObject *parent = 0); + +signals: + void itemMoved(DiagramItem *movedItem, const QPointF &movedFromPosition); + +protected: + void mousePressEvent(QGraphicsSceneMouseEvent *event); + void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); + +private: + QGraphicsItem *movingItem; + QPointF oldPos; +}; +//! [0] + +#endif diff --git a/examples/widgets/tools/undoframework/images/cross.png b/examples/widgets/tools/undoframework/images/cross.png Binary files differnew file mode 100644 index 0000000000..09e5e8c2ad --- /dev/null +++ b/examples/widgets/tools/undoframework/images/cross.png diff --git a/examples/widgets/tools/undoframework/main.cpp b/examples/widgets/tools/undoframework/main.cpp new file mode 100644 index 0000000000..19ba6ec095 --- /dev/null +++ b/examples/widgets/tools/undoframework/main.cpp @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include "mainwindow.h" + +//! [0] +int main(int argv, char *args[]) +{ + Q_INIT_RESOURCE(undoframework); + + QApplication app(argv, args); + + MainWindow mainWindow; + mainWindow.show(); + + return app.exec(); +} +//! [0] diff --git a/examples/widgets/tools/undoframework/mainwindow.cpp b/examples/widgets/tools/undoframework/mainwindow.cpp new file mode 100644 index 0000000000..6ac3818113 --- /dev/null +++ b/examples/widgets/tools/undoframework/mainwindow.cpp @@ -0,0 +1,206 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets> + +#include "mainwindow.h" +#include "diagramscene.h" +#include "diagramitem.h" +#include "commands.h" + +//! [0] +MainWindow::MainWindow() +{ + undoStack = new QUndoStack(this); + + createActions(); + createMenus(); + + createUndoView(); + + diagramScene = new DiagramScene(); + QBrush pixmapBrush(QPixmap(":/images/cross.png").scaled(30, 30)); + diagramScene->setBackgroundBrush(pixmapBrush); + diagramScene->setSceneRect(QRect(0, 0, 500, 500)); + + connect(diagramScene, SIGNAL(itemMoved(DiagramItem*,QPointF)), + this, SLOT(itemMoved(DiagramItem*,QPointF))); + + setWindowTitle("Undo Framework"); + QGraphicsView *view = new QGraphicsView(diagramScene); + setCentralWidget(view); + resize(700, 500); +} +//! [0] + +//! [1] +void MainWindow::createUndoView() +{ + undoView = new QUndoView(undoStack); + undoView->setWindowTitle(tr("Command List")); + undoView->show(); + undoView->setAttribute(Qt::WA_QuitOnClose, false); +} +//! [1] + +//! [2] +void MainWindow::createActions() +{ + deleteAction = new QAction(tr("&Delete Item"), this); + deleteAction->setShortcut(tr("Del")); + connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteItem())); +//! [2] //! [3] + +//! [3] //! [4] + addBoxAction = new QAction(tr("Add &Box"), this); +//! [4] + addBoxAction->setShortcut(tr("Ctrl+O")); + connect(addBoxAction, SIGNAL(triggered()), this, SLOT(addBox())); + + addTriangleAction = new QAction(tr("Add &Triangle"), this); + addTriangleAction->setShortcut(tr("Ctrl+T")); + connect(addTriangleAction, SIGNAL(triggered()), this, SLOT(addTriangle())); + +//! [5] + undoAction = undoStack->createUndoAction(this, tr("&Undo")); + undoAction->setShortcuts(QKeySequence::Undo); + + redoAction = undoStack->createRedoAction(this, tr("&Redo")); + redoAction->setShortcuts(QKeySequence::Redo); +//! [5] + + exitAction = new QAction(tr("E&xit"), this); + exitAction->setShortcuts(QKeySequence::Quit); + connect(exitAction, SIGNAL(triggered()), this, SLOT(close())); + + aboutAction = new QAction(tr("&About"), this); + QList<QKeySequence> aboutShortcuts; + aboutShortcuts << tr("Ctrl+A") << tr("Ctrl+B"); + aboutAction->setShortcuts(aboutShortcuts); + connect(aboutAction, SIGNAL(triggered()), this, SLOT(about())); +} + +//! [6] +void MainWindow::createMenus() +{ +//! [6] + fileMenu = menuBar()->addMenu(tr("&File")); + fileMenu->addAction(exitAction); + +//! [7] + editMenu = menuBar()->addMenu(tr("&Edit")); + editMenu->addAction(undoAction); + editMenu->addAction(redoAction); + editMenu->addSeparator(); + editMenu->addAction(deleteAction); + connect(editMenu, SIGNAL(aboutToShow()), + this, SLOT(itemMenuAboutToShow())); + connect(editMenu, SIGNAL(aboutToHide()), + this, SLOT(itemMenuAboutToHide())); + +//! [7] + itemMenu = menuBar()->addMenu(tr("&Item")); + itemMenu->addAction(addBoxAction); + itemMenu->addAction(addTriangleAction); + + helpMenu = menuBar()->addMenu(tr("&About")); + helpMenu->addAction(aboutAction); +//! [8] +} +//! [8] + +//! [9] +void MainWindow::itemMoved(DiagramItem *movedItem, + const QPointF &oldPosition) +{ + undoStack->push(new MoveCommand(movedItem, oldPosition)); +} +//! [9] + +//! [10] +void MainWindow::deleteItem() +{ + if (diagramScene->selectedItems().isEmpty()) + return; + + QUndoCommand *deleteCommand = new DeleteCommand(diagramScene); + undoStack->push(deleteCommand); +} +//! [10] + +//! [11] +void MainWindow::itemMenuAboutToHide() +{ + deleteAction->setEnabled(true); +} +//! [11] + +//! [12] +void MainWindow::itemMenuAboutToShow() +{ + deleteAction->setEnabled(!diagramScene->selectedItems().isEmpty()); +} +//! [12] + +//! [13] +void MainWindow::addBox() +{ + QUndoCommand *addCommand = new AddCommand(DiagramItem::Box, diagramScene); + undoStack->push(addCommand); +} +//! [13] + +//! [14] +void MainWindow::addTriangle() +{ + QUndoCommand *addCommand = new AddCommand(DiagramItem::Triangle, + diagramScene); + undoStack->push(addCommand); +} +//! [14] + +//! [15] +void MainWindow::about() +{ + QMessageBox::about(this, tr("About Undo"), + tr("The <b>Undo</b> example demonstrates how to " + "use Qt's undo framework.")); +} +//! [15] diff --git a/examples/widgets/tools/undoframework/mainwindow.h b/examples/widgets/tools/undoframework/mainwindow.h new file mode 100644 index 0000000000..f61b0abb1b --- /dev/null +++ b/examples/widgets/tools/undoframework/mainwindow.h @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> + +QT_BEGIN_NAMESPACE +class QAction; +class QToolBar; +class QMenu; +class QUndoStack; +class QUndoView; +QT_END_NAMESPACE +class DiagramScene; +class DiagramItem; + +//! [0] +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +public slots: + void itemMoved(DiagramItem *movedDiagram, const QPointF &moveStartPosition); + +private slots: + void deleteItem(); + void addBox(); + void addTriangle(); + void about(); + void itemMenuAboutToShow(); + void itemMenuAboutToHide(); + +private: + void createActions(); + void createMenus(); + void createUndoView(); + + QAction *deleteAction; + QAction *addBoxAction; + QAction *addTriangleAction; + QAction *undoAction; + QAction *redoAction; + QAction *exitAction; + QAction *aboutAction; + + QMenu *fileMenu; + QMenu *editMenu; + QMenu *itemMenu; + QMenu *helpMenu; + + DiagramScene *diagramScene; + QUndoStack *undoStack; + QUndoView *undoView; +}; +//! [0] + +#endif diff --git a/examples/widgets/tools/undoframework/undoframework.desktop b/examples/widgets/tools/undoframework/undoframework.desktop new file mode 100644 index 0000000000..24b7f320f4 --- /dev/null +++ b/examples/widgets/tools/undoframework/undoframework.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=Undo Framework +Exec=/opt/usr/bin/undoframework +Icon=undoframework +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/widgets/tools/undoframework/undoframework.pro b/examples/widgets/tools/undoframework/undoframework.pro new file mode 100644 index 0000000000..d50247442a --- /dev/null +++ b/examples/widgets/tools/undoframework/undoframework.pro @@ -0,0 +1,20 @@ +HEADERS = commands.h \ + diagramitem.h \ + diagramscene.h \ + mainwindow.h +SOURCES = commands.cpp \ + diagramitem.cpp \ + diagramscene.cpp \ + main.cpp \ + mainwindow.cpp +RESOURCES = undoframework.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tools/undoframework +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS undoframework.pro README images +sources.path = $$[QT_INSTALL_EXAMPLES]/tools/undoframework +INSTALLS += target sources + +QT += widgets + +simulator: warning(This example might not fully work on Simulator platform) diff --git a/examples/widgets/tools/undoframework/undoframework.qrc b/examples/widgets/tools/undoframework/undoframework.qrc new file mode 100644 index 0000000000..6321d94d8d --- /dev/null +++ b/examples/widgets/tools/undoframework/undoframework.qrc @@ -0,0 +1,6 @@ +<!DOCTYPE RCC><RCC version="1.0"> + <qresource> + <file>images/cross.png</file> + </qresource> + </RCC> + diff --git a/examples/widgets/widgets.pro b/examples/widgets/widgets.pro index 4f527b1540..d6d86dd3a6 100644 --- a/examples/widgets/widgets.pro +++ b/examples/widgets/widgets.pro @@ -15,6 +15,7 @@ SUBDIRS = \ richtext \ scroller \ statemachine \ + tools \ tutorials \ widgets @@ -23,4 +24,4 @@ contains(DEFINES, QT_NO_CURSOR): SUBDIRS -= mainwindows # install sources.files = *.pro sources.path = $$[QT_INSTALL_EXAMPLES]/widgets -INSTALLS += sources
\ No newline at end of file +INSTALLS += sources |