1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the demonstration applications of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
41 #include "macmainwindow.h"
42 #import <Cocoa/Cocoa.h>
48 #include <Carbon/Carbon.h>
52 SearchWidget::SearchWidget(QWidget *parent)
53 : QMacCocoaViewContainer(0, parent)
55 // Many Cocoa objects create temporary autorelease objects,
56 // so create a pool to catch them.
57 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
59 // Create the NSSearchField, set it on the QCocoaViewContainer.
60 NSSearchField *search = [[NSSearchField alloc] init];
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];
68 // Release our reference, since our super class takes ownership and we
69 // don't need it anymore.
72 // Clean up our pool as we no longer need it.
77 SearchWidget::~SearchWidget()
81 QSize SearchWidget::sizeHint() const
83 return QSize(150, 40);
87 QMenu *createMenu(QWidget *parent)
89 QMenu *searchMenu = new QMenu(parent);
91 QAction * indexAction = searchMenu->addAction("Index Search");
92 indexAction->setCheckable(true);
93 indexAction->setChecked(true);
95 QAction * fulltextAction = searchMenu->addAction("Full Text Search");
96 fulltextAction->setCheckable(true);
98 QActionGroup *searchActionGroup = new QActionGroup(parent);
99 searchActionGroup->addAction(indexAction);
100 searchActionGroup->addAction(fulltextAction);
101 searchActionGroup->setExclusive(true);
106 SearchWrapper::SearchWrapper(QWidget *parent)
109 s = new SearchWidget(this);
111 setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
114 QSize SearchWrapper::sizeHint() const
116 return s->sizeHint() + QSize(6, 2);
119 Spacer::Spacer(QWidget *parent)
122 QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
123 setSizePolicy(sizePolicy);
126 QSize Spacer::sizeHint() const
131 MacSplitterHandle::MacSplitterHandle(Qt::Orientation orientation, QSplitter *parent)
132 : QSplitterHandle(orientation, parent) { }
134 // Paint the horizontal handle as a gradient, paint
135 // the vertical handle as a line.
136 void MacSplitterHandle::paintEvent(QPaintEvent *)
138 QPainter painter(this);
140 QColor topColor(145, 145, 145);
141 QColor bottomColor(142, 142, 142);
142 QColor gradientStart(252, 252, 252);
143 QColor gradientStop(223, 223, 223);
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);
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));
156 painter.setPen(topColor);
157 painter.drawLine(0, 0, 0, height());
161 QSize MacSplitterHandle::sizeHint() const
163 QSize parent = QSplitterHandle::sizeHint();
164 if (orientation() == Qt::Vertical) {
165 return parent + QSize(0, 3);
167 return QSize(1, parent.height());
171 QSplitterHandle *MacSplitter::createHandle()
173 return new MacSplitterHandle(orientation(), this);
176 MacMainWindow::MacMainWindow()
179 restoreGeometry(settings.value("Geometry").toByteArray());
181 setWindowTitle("Mac Main Window");
183 splitter = new MacSplitter();
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);
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);
199 sidebar->setModel(createItemModel());
200 sidebar->header()->hide();
201 sidebar->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
202 sidebar->setTextElideMode(Qt::ElideMiddle);
204 splitter->addWidget(sidebar);
206 horizontalSplitter = new MacSplitter();
207 horizontalSplitter->setOrientation(Qt::Vertical);
208 splitter->addWidget(horizontalSplitter);
210 splitter->setStretchFactor(0, 0);
211 splitter->setStretchFactor(1, 1);
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);
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>");
233 horizontalSplitter->addWidget(textedit);
235 setCentralWidget(splitter);
237 toolBar = addToolBar(tr("Search"));
238 toolBar->addWidget(new Spacer());
239 toolBar->addWidget(new SearchWrapper());
241 setUnifiedTitleAndToolBarOnMac(true);
244 MacMainWindow::~MacMainWindow()
247 settings.setValue("Geometry", saveGeometry());
250 QAbstractItemModel *MacMainWindow::createItemModel()
252 QStandardItemModel *model = new QStandardItemModel();
253 QStandardItem *parentItem = model->invisibleRootItem();
255 QStandardItem *documentationItem = new QStandardItem("Documentation");
256 parentItem->appendRow(documentationItem);
258 QStandardItem *assistantItem = new QStandardItem("Qt MainWindow Manual");
259 documentationItem->appendRow(assistantItem);
261 QStandardItem *designerItem = new QStandardItem("Qt Designer Manual");
262 documentationItem->appendRow(designerItem);
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);
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"));
279 void MacMainWindow::resizeEvent(QResizeEvent *)
282 toolBar->updateGeometry();
285 QAbstractItemModel *MacMainWindow::createDocumentModel()
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"));