fce87ae0a151c2c4d04d7000235d149073b47512
[profile/ivi/qtbase.git] / examples / mainwindows / macmainwindow / macmainwindow.mm
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the demonstration applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include "macmainwindow.h"
42 #import <Cocoa/Cocoa.h>
43 #include <QtGui>
44
45
46 #ifdef Q_WS_MAC
47
48 #include <Carbon/Carbon.h>
49
50
51 //![0]
52 SearchWidget::SearchWidget(QWidget *parent)
53     : QMacCocoaViewContainer(0, parent)
54 {
55     // Many Cocoa objects create temporary autorelease objects,
56     // so create a pool to catch them.
57     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
58
59     // Create the NSSearchField, set it on the QCocoaViewContainer.
60     NSSearchField *search = [[NSSearchField alloc] init];
61     setCocoaView(search);
62
63     // Use a Qt menu for the search field menu.
64     QMenu *qtMenu = createMenu(this);
65     NSMenu *nsMenu = qtMenu->macMenu(0);
66     [[search cell] setSearchMenuTemplate:nsMenu];
67
68     // Release our reference, since our super class takes ownership and we
69     // don't need it anymore.
70     [search release];
71
72     // Clean up our pool as we no longer need it.
73     [pool release];
74 }
75 //![0]
76
77 SearchWidget::~SearchWidget()
78 {
79 }
80
81 QSize SearchWidget::sizeHint() const
82 {
83     return QSize(150, 40);
84 }
85
86
87 QMenu *createMenu(QWidget *parent)
88 {
89     QMenu *searchMenu = new QMenu(parent);
90     
91     QAction * indexAction = searchMenu->addAction("Index Search");
92     indexAction->setCheckable(true);
93     indexAction->setChecked(true);
94
95     QAction * fulltextAction = searchMenu->addAction("Full Text Search");
96     fulltextAction->setCheckable(true);
97
98     QActionGroup *searchActionGroup = new QActionGroup(parent);
99     searchActionGroup->addAction(indexAction);
100     searchActionGroup->addAction(fulltextAction);
101     searchActionGroup->setExclusive(true);
102     
103     return searchMenu;
104 }
105
106 SearchWrapper::SearchWrapper(QWidget *parent)
107 :QWidget(parent)
108 {
109     s = new SearchWidget(this);
110     s->move(2,2);
111     setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
112 }
113
114 QSize SearchWrapper::sizeHint() const
115 {
116     return s->sizeHint() + QSize(6, 2);
117 }
118
119 Spacer::Spacer(QWidget *parent)
120 :QWidget(parent)
121 {
122     QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
123     setSizePolicy(sizePolicy);
124 }
125
126 QSize Spacer::sizeHint() const
127 {
128     return QSize(1, 1);
129 }
130
131 MacSplitterHandle::MacSplitterHandle(Qt::Orientation orientation, QSplitter *parent)
132 : QSplitterHandle(orientation, parent) {   }
133
134 // Paint the horizontal handle as a gradient, paint
135 // the vertical handle as a line.
136 void MacSplitterHandle::paintEvent(QPaintEvent *)
137 {
138     QPainter painter(this);
139
140     QColor topColor(145, 145, 145);
141     QColor bottomColor(142, 142, 142);
142     QColor gradientStart(252, 252, 252);
143     QColor gradientStop(223, 223, 223);
144
145     if (orientation() == Qt::Vertical) {
146         painter.setPen(topColor);
147         painter.drawLine(0, 0, width(), 0);
148         painter.setPen(bottomColor);
149         painter.drawLine(0, height() - 1, width(), height() - 1);
150
151         QLinearGradient linearGrad(QPointF(0, 0), QPointF(0, height() -3));
152         linearGrad.setColorAt(0, gradientStart);
153         linearGrad.setColorAt(1, gradientStop);
154         painter.fillRect(QRect(QPoint(0,1), size() - QSize(0, 2)), QBrush(linearGrad));
155     } else {
156         painter.setPen(topColor);
157         painter.drawLine(0, 0, 0, height());
158     }
159 }
160
161 QSize MacSplitterHandle::sizeHint() const
162 {
163     QSize parent = QSplitterHandle::sizeHint();
164     if (orientation() == Qt::Vertical) {
165         return parent + QSize(0, 3);
166     } else {
167         return QSize(1, parent.height());
168     }
169 }
170
171 QSplitterHandle *MacSplitter::createHandle()
172 {
173     return new MacSplitterHandle(orientation(), this);
174 }
175
176 MacMainWindow::MacMainWindow()
177 {
178     QSettings settings;
179     restoreGeometry(settings.value("Geometry").toByteArray());
180
181     setWindowTitle("Mac Main Window");
182
183     splitter = new MacSplitter();
184
185     // Set up the left-hand side blue side bar.
186     sidebar = new QTreeView();
187     sidebar->setFrameStyle(QFrame::NoFrame);
188     sidebar->setAttribute(Qt::WA_MacShowFocusRect, false);
189     sidebar->setAutoFillBackground(true);
190     
191     // Set the palette.
192     QPalette palette = sidebar->palette();
193     QColor macSidebarColor(231, 237, 246);
194     QColor macSidebarHighlightColor(168, 183, 205);
195     palette.setColor(QPalette::Base, macSidebarColor);
196     palette.setColor(QPalette::Highlight, macSidebarHighlightColor);
197     sidebar->setPalette(palette);
198
199     sidebar->setModel(createItemModel());
200     sidebar->header()->hide();
201     sidebar->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
202     sidebar->setTextElideMode(Qt::ElideMiddle);
203
204     splitter->addWidget(sidebar);
205
206     horizontalSplitter = new MacSplitter();
207     horizontalSplitter->setOrientation(Qt::Vertical);
208     splitter->addWidget(horizontalSplitter);
209     
210     splitter->setStretchFactor(0, 0);
211     splitter->setStretchFactor(1, 1);
212
213     // Set up the top document list view.
214     documents = new QListView();
215     documents->setFrameStyle(QFrame::NoFrame);
216     documents->setAttribute(Qt::WA_MacShowFocusRect, false);
217     documents->setModel(createDocumentModel());
218     documents->setAlternatingRowColors(true);
219     documents->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
220     horizontalSplitter->addWidget(documents);
221     horizontalSplitter->setStretchFactor(0, 0);
222
223     // Set up the text view.
224     textedit = new QTextEdit();
225     textedit->setFrameStyle(QFrame::NoFrame);
226     textedit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
227     textedit->setText("<br><br><br><br><br><br><center><b>This demo shows how to create a \
228                        Qt main window application that has the same appearance as other \
229                        Mac OS X applications such as Mail or iTunes. This includes \
230                        customizing the item views and QSplitter and wrapping native widgets \
231                        such as the search field.</b></center>");
232
233     horizontalSplitter->addWidget(textedit);
234
235     setCentralWidget(splitter);
236
237     toolBar = addToolBar(tr("Search"));
238     toolBar->addWidget(new Spacer());
239     toolBar->addWidget(new SearchWrapper());
240     
241     setUnifiedTitleAndToolBarOnMac(true);
242 }
243
244 MacMainWindow::~MacMainWindow()
245 {
246     QSettings settings;
247     settings.setValue("Geometry", saveGeometry());
248 }
249
250 QAbstractItemModel *MacMainWindow::createItemModel()
251 {
252     QStandardItemModel *model = new QStandardItemModel();
253     QStandardItem *parentItem = model->invisibleRootItem();
254
255     QStandardItem *documentationItem = new QStandardItem("Documentation"); 
256     parentItem->appendRow(documentationItem);
257
258     QStandardItem *assistantItem = new QStandardItem("Qt MainWindow Manual"); 
259     documentationItem->appendRow(assistantItem);
260
261     QStandardItem *designerItem = new QStandardItem("Qt Designer Manual"); 
262     documentationItem->appendRow(designerItem);
263
264     QStandardItem *qtItem = new QStandardItem("Qt Reference Documentation"); 
265     qtItem->appendRow(new QStandardItem("Classes"));
266     qtItem->appendRow(new QStandardItem("Overviews"));
267     qtItem->appendRow(new QStandardItem("Tutorial & Examples"));
268     documentationItem->appendRow(qtItem);
269
270     QStandardItem *bookmarksItem = new QStandardItem("Bookmarks"); 
271     parentItem->appendRow(bookmarksItem);
272     bookmarksItem->appendRow(new QStandardItem("QWidget"));
273     bookmarksItem->appendRow(new QStandardItem("QObject"));
274     bookmarksItem->appendRow(new QStandardItem("QWizard"));
275
276     return model;
277 }
278
279 void MacMainWindow::resizeEvent(QResizeEvent *)
280 {
281     if (toolBar)
282         toolBar->updateGeometry();
283 }
284
285 QAbstractItemModel *MacMainWindow::createDocumentModel()
286 {
287     QStandardItemModel *model = new QStandardItemModel();
288     QStandardItem *parentItem = model->invisibleRootItem(); 
289     parentItem->appendRow(new QStandardItem("QWidget Class Reference"));
290     parentItem->appendRow(new QStandardItem("QObject Class Reference"));
291     parentItem->appendRow(new QStandardItem("QListView Class Reference"));
292
293     return model;
294 }
295
296 #endif // Q_WS_MAC