diff options
author | Jake Petroules <jake.petroules@petroules.com> | 2013-01-04 14:12:59 -0500 |
---|---|---|
committer | Morten Johan Sørvig <morten.sorvig@digia.com> | 2013-01-07 14:11:33 +0100 |
commit | d8020c2df62f65b76de24c5752191cf6fa03727a (patch) | |
tree | c7d17fef135e588388e0512d1cbd604ef3ec18f6 /examples | |
parent | 245db5536b6c8b0f6944846f31b3c889a3b6caf0 (diff) |
Add a preferences window to the macunifiedtoolbar app.
This demonstrates the usage of the setUnifiedTitleAndToolBarOnMac()
function.
Change-Id: I4ca7d74ef7be05c87c8bf453cdd1cdad7ceb4dbc
Reviewed-by: Morten Johan Sørvig <morten.sorvig@digia.com>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/macunifiedtoolbar/macunifiedtoolbar.pro | 18 | ||||
-rw-r--r-- | examples/macunifiedtoolbar/preferenceswindow.cpp | 107 | ||||
-rw-r--r-- | examples/macunifiedtoolbar/preferenceswindow.h | 69 | ||||
-rw-r--r-- | examples/macunifiedtoolbar/preferenceswindow.ui | 209 | ||||
-rw-r--r-- | examples/macunifiedtoolbar/window.cpp | 12 |
5 files changed, 408 insertions, 7 deletions
diff --git a/examples/macunifiedtoolbar/macunifiedtoolbar.pro b/examples/macunifiedtoolbar/macunifiedtoolbar.pro index a780f68..99101c4 100644 --- a/examples/macunifiedtoolbar/macunifiedtoolbar.pro +++ b/examples/macunifiedtoolbar/macunifiedtoolbar.pro @@ -1,18 +1,22 @@ include (../../src/qtmacextras.pri) +QT += core gui +greaterThan(QT_MAJOR_VERSION, 4):QT += widgets -QT += widgets -SOURCES += main.cpp \ +SOURCES += \ + main.cpp \ + preferenceswindow.cpp \ window.cpp -RESOURCES += \ - macunifiedtoolbar.qrc - -ICON = qtlogo.icns - HEADERS += \ + preferenceswindow.h \ window.h FORMS += \ + preferenceswindow.ui \ window.ui +RESOURCES += \ + macunifiedtoolbar.qrc + +ICON = qtlogo.icns diff --git a/examples/macunifiedtoolbar/preferenceswindow.cpp b/examples/macunifiedtoolbar/preferenceswindow.cpp new file mode 100644 index 0000000..4855e53 --- /dev/null +++ b/examples/macunifiedtoolbar/preferenceswindow.cpp @@ -0,0 +1,107 @@ +/**************************************************************************** + ** + ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). + ** Contact: http://www.qt-project.org/legal + ** + ** This file is part of QtMacExtras in 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 "preferenceswindow.h" +#include "ui_preferenceswindow.h" +#include "qtmacunifiedtoolbar.h" +#include <QTimer> + +PreferencesWindow::PreferencesWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::PreferencesWindow) +{ + ui->setupUi(this); + + // Ensure we can only select one 'tab' at a time + QActionGroup *items = new QActionGroup(this); + foreach (QAction *action, ui->toolBar->actions()) + items->addAction(action); + + // This single line of code is all that's needed to transform a QToolBar into + // a QtMacUnifiedToolBar. It is available on all platforms and is simply a no-op + // on platforms other than OS X. + ::setUnifiedTitleAndToolBarOnMac(ui->toolBar, ui->useNativeToolbarCheckBox->isChecked()); + + // Initial setup + ui->actionGeneral->trigger(); + + QTimer::singleShot(0, this, SLOT(pack())); +} + +PreferencesWindow::~PreferencesWindow() +{ + delete ui; +} + +void PreferencesWindow::toolbarItemTriggered() +{ + QAction *action = qobject_cast<QAction*>(sender()); + if (action) + { + setWindowTitle(action->text()); + } + + if (sender() == ui->actionGeneral) + { + ui->stackedWidget->setCurrentWidget(ui->generalPage); + } + else if (sender() == ui->actionNetwork) + { + ui->stackedWidget->setCurrentWidget(ui->networkPage); + } + else if (sender() == ui->actionAdvanced) + { + ui->stackedWidget->setCurrentWidget(ui->advancedPage); + } + + QTimer::singleShot(0, this, SLOT(pack())); +} + +void PreferencesWindow::useNativeToolBarToggled(bool on) +{ + ::setUnifiedTitleAndToolBarOnMac(ui->toolBar, on); + QTimer::singleShot(0, this, SLOT(pack())); +} + +void PreferencesWindow::pack() +{ + resize(QSize()); +} diff --git a/examples/macunifiedtoolbar/preferenceswindow.h b/examples/macunifiedtoolbar/preferenceswindow.h new file mode 100644 index 0000000..ade07b5 --- /dev/null +++ b/examples/macunifiedtoolbar/preferenceswindow.h @@ -0,0 +1,69 @@ +/**************************************************************************** + ** + ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). + ** Contact: http://www.qt-project.org/legal + ** + ** This file is part of QtMacExtras in 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 PREFERENCESWINDOW_H +#define PREFERENCESWINDOW_H + +#include <QMainWindow> + +namespace Ui +{ + class PreferencesWindow; +} + +class PreferencesWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit PreferencesWindow(QWidget *parent = 0); + ~PreferencesWindow(); + +private slots: + void toolbarItemTriggered(); + void useNativeToolBarToggled(bool); + void pack(); + +private: + Ui::PreferencesWindow *ui; +}; + +#endif // PREFERENCESWINDOW_H diff --git a/examples/macunifiedtoolbar/preferenceswindow.ui b/examples/macunifiedtoolbar/preferenceswindow.ui new file mode 100644 index 0000000..7f3ef00 --- /dev/null +++ b/examples/macunifiedtoolbar/preferenceswindow.ui @@ -0,0 +1,209 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>PreferencesWindow</class> + <widget class="QMainWindow" name="PreferencesWindow"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>400</width> + <height>300</height> + </rect> + </property> + <property name="windowTitle"> + <string>Preferences</string> + </property> + <widget class="QWidget" name="centralwidget"> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <widget class="QCheckBox" name="useNativeToolbarCheckBox"> + <property name="text"> + <string>Use native NSToolbar</string> + </property> + <property name="checked"> + <bool>true</bool> + </property> + </widget> + </item> + <item> + <widget class="QStackedWidget" name="stackedWidget"> + <widget class="QWidget" name="generalPage"> + <layout class="QGridLayout" name="gridLayout_2"> + <item row="0" column="0"> + <widget class="QLabel" name="generalLabel"> + <property name="text"> + <string>General preferences would go here</string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + </layout> + </widget> + <widget class="QWidget" name="networkPage"> + <layout class="QGridLayout" name="gridLayout_3"> + <item row="0" column="0"> + <widget class="QLabel" name="networkLabel"> + <property name="text"> + <string>Network preferences would go here</string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + </layout> + </widget> + <widget class="QWidget" name="advancedPage"> + <layout class="QGridLayout" name="gridLayout"> + <item row="0" column="0"> + <widget class="QLabel" name="advancedLabel"> + <property name="text"> + <string>Advanced preferences would go here</string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + </layout> + </widget> + </widget> + </item> + </layout> + </widget> + <widget class="QToolBar" name="toolBar"> + <property name="movable"> + <bool>false</bool> + </property> + <property name="toolButtonStyle"> + <enum>Qt::ToolButtonTextUnderIcon</enum> + </property> + <property name="floatable"> + <bool>false</bool> + </property> + <attribute name="toolBarArea"> + <enum>TopToolBarArea</enum> + </attribute> + <attribute name="toolBarBreak"> + <bool>false</bool> + </attribute> + <addaction name="actionGeneral"/> + <addaction name="actionNetwork"/> + <addaction name="actionAdvanced"/> + </widget> + <action name="actionGeneral"> + <property name="checkable"> + <bool>true</bool> + </property> + <property name="checked"> + <bool>true</bool> + </property> + <property name="icon"> + <iconset resource="macunifiedtoolbar.qrc"> + <normaloff>:/qtlogo.png</normaloff>:/qtlogo.png</iconset> + </property> + <property name="text"> + <string>General</string> + </property> + </action> + <action name="actionNetwork"> + <property name="checkable"> + <bool>true</bool> + </property> + <property name="icon"> + <iconset resource="macunifiedtoolbar.qrc"> + <normaloff>:/qtlogo.png</normaloff>:/qtlogo.png</iconset> + </property> + <property name="text"> + <string>Network</string> + </property> + </action> + <action name="actionAdvanced"> + <property name="checkable"> + <bool>true</bool> + </property> + <property name="icon"> + <iconset resource="macunifiedtoolbar.qrc"> + <normaloff>:/qtlogo.png</normaloff>:/qtlogo.png</iconset> + </property> + <property name="text"> + <string>Advanced</string> + </property> + </action> + </widget> + <resources> + <include location="macunifiedtoolbar.qrc"/> + </resources> + <connections> + <connection> + <sender>actionGeneral</sender> + <signal>triggered()</signal> + <receiver>PreferencesWindow</receiver> + <slot>toolbarItemTriggered()</slot> + <hints> + <hint type="sourcelabel"> + <x>-1</x> + <y>-1</y> + </hint> + <hint type="destinationlabel"> + <x>399</x> + <y>299</y> + </hint> + </hints> + </connection> + <connection> + <sender>actionNetwork</sender> + <signal>triggered()</signal> + <receiver>PreferencesWindow</receiver> + <slot>toolbarItemTriggered()</slot> + <hints> + <hint type="sourcelabel"> + <x>-1</x> + <y>-1</y> + </hint> + <hint type="destinationlabel"> + <x>399</x> + <y>299</y> + </hint> + </hints> + </connection> + <connection> + <sender>actionAdvanced</sender> + <signal>triggered()</signal> + <receiver>PreferencesWindow</receiver> + <slot>toolbarItemTriggered()</slot> + <hints> + <hint type="sourcelabel"> + <x>-1</x> + <y>-1</y> + </hint> + <hint type="destinationlabel"> + <x>399</x> + <y>299</y> + </hint> + </hints> + </connection> + <connection> + <sender>useNativeToolbarCheckBox</sender> + <signal>toggled(bool)</signal> + <receiver>PreferencesWindow</receiver> + <slot>useNativeToolBarToggled(bool)</slot> + <hints> + <hint type="sourcelabel"> + <x>61</x> + <y>87</y> + </hint> + <hint type="destinationlabel"> + <x>199</x> + <y>149</y> + </hint> + </hints> + </connection> + </connections> + <slots> + <slot>toolbarItemTriggered()</slot> + <slot>useNativeToolBarToggled(bool)</slot> + </slots> +</ui> diff --git a/examples/macunifiedtoolbar/window.cpp b/examples/macunifiedtoolbar/window.cpp index 8fd00bc..6c79b06 100644 --- a/examples/macunifiedtoolbar/window.cpp +++ b/examples/macunifiedtoolbar/window.cpp @@ -41,13 +41,17 @@ #include "window.h" #include "ui_window.h" +#include "preferenceswindow.h" #include "qtmacunifiedtoolbar.h" #include <QDesktopWidget> +#include <QMenuBar> #include <QTimer> class WindowPrivate { public: + PreferencesWindow *preferencesWindow; + QMenuBar *mainMenuBar; QtMacUnifiedToolBar *toolBar; }; @@ -58,6 +62,12 @@ Window::Window(QWidget *parent) : { ui->setupUi(this); + d->preferencesWindow = new PreferencesWindow(); + + d->mainMenuBar = new QMenuBar(); + QMenu *toolsMenu = d->mainMenuBar->addMenu("Tools"); + toolsMenu->addAction("Options", d->preferencesWindow, SLOT(show())); + d->toolBar = new QtMacUnifiedToolBar(this); d->toolBar->addAction(QIcon(":/qtlogo.png"), "Hello"); d->toolBar->addAction(QIcon(":/qtlogo.png"), "World"); @@ -99,6 +109,8 @@ Window::Window(QWidget *parent) : Window::~Window() { delete ui; + delete d->mainMenuBar; + delete d->preferencesWindow; delete d; } |