Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add a QToolbarTabWidget implementation

  • Loading branch information...
commit 8ff41b5b28d57c5d980bc691e5d7b53940a74a7a 1 parent 569fc55
@lfranchi authored
View
6 CMakeLists.txt
@@ -14,21 +14,25 @@ set(HEADERS
qsearchfield.h
qbutton.h
qprogressindicatorspinning.h
+ qtoolbartabwidget.h
)
qt4_wrap_cpp(MOC_SOURCES ${HEADERS})
+qt4_add_resources(GALLERY_RC_SRCS "resources.qrc")
if(APPLE)
list(APPEND SOURCES
qsearchfield_mac.mm
qbutton_mac.mm
qprogressindicatorspinning_mac.mm
+ qtoolbartabwidget_mac.mm
)
else()
list(APPEND SOURCES
qsearchfield_nonmac.cpp
qbutton_nonmac.cpp
qprogressindicatorspinning_nonmac.cpp
+ qtoolbartabwidget_nonmac.cpp
)
set(RESOURCES
qsearchfield_nonmac.qrc
@@ -39,7 +43,7 @@ endif()
add_executable(Qocoa
WIN32 MACOSX_BUNDLE
- ${SOURCES} ${MOC_SOURCES} ${RESOURCES_SOURCES}
+ ${SOURCES} ${MOC_SOURCES} ${RESOURCES_SOURCES} ${GALLERY_RC_SRCS}
)
target_link_libraries(Qocoa ${QT_LIBRARIES})
View
11 Qocoa.pro
@@ -1,17 +1,22 @@
SOURCES += main.cpp\
- gallery.cpp \
+ gallery.cpp
HEADERS += gallery.h \
qocoa_mac.h \
qsearchfield.h \
qbutton.h \
qprogressindicatorspinning.h \
+ qtoolbartabdialog.h
+
+RESOURCES += resources.qrc
+CONFIG += debug
mac {
- OBJECTIVE_SOURCES += qsearchfield_mac.mm qbutton_mac.mm qprogressindicatorspinning_mac.mm
+ OBJECTIVE_SOURCES += qsearchfield_mac.mm qbutton_mac.mm qprogressindicatorspinning_mac.mm qtoolbartabdialog_mac.mm
LIBS += -framework Foundation -framework Appkit
QMAKE_CFLAGS += -mmacosx-version-min=10.6
} else {
- SOURCES += qsearchfield_nonmac.cpp qbutton_nonmac.cpp qprogressindicatorspinning_nonmac.cpp
+ HEADERS += qtoolbartabdialog_nonmac.cpp
+ SOURCES += qsearchfield_nonmac.cpp qbutton_nonmac.cpp qprogressindicatorspinning_nonmac.cpp qtoolbartabdialog_nonmac.cpp
RESOURCES += qsearchfield_nonmac.qrc qprogressindicatorspinning_nonmac.qrc
}
View
BIN  bookmarks.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
71 gallery.cpp
@@ -1,12 +1,22 @@
#include "gallery.h"
#include <QVBoxLayout>
+#include <QDialog>
+#include <QDialogButtonBox>
+#include <QIcon>
+#include <QTextEdit>
+#include <QPushButton>
+#include <QDebug>
+#include <QListWidget>
+#include <QGroupBox>
+#include <QLineEdit>
#include "qsearchfield.h"
#include "qbutton.h"
#include "qprogressindicatorspinning.h"
+#include "qtoolbartabdialog.h"
-Gallery::Gallery(QWidget *parent) : QWidget(parent)
+Gallery::Gallery(QWidget *parent) : QWidget(parent), m_toolbarTabDialog(0)
{
setWindowTitle("Qocoa Gallery");
QVBoxLayout *layout = new QVBoxLayout(this);
@@ -68,8 +78,65 @@ Gallery::Gallery(QWidget *parent) : QWidget(parent)
layout->addWidget(inlineButton);
#endif
-
QProgressIndicatorSpinning *progressIndicatorSpinning = new QProgressIndicatorSpinning(this);
progressIndicatorSpinning->animate();
layout->addWidget(progressIndicatorSpinning);
+
+ QButton *openTabWidget = new QButton(this, QButton::Rounded);
+ openTabWidget->setText("Toolbar Tab Widget");
+ connect(openTabWidget, SIGNAL(clicked(bool)), this, SLOT(showTabToolbarWidget()));
+ layout->addWidget(openTabWidget);
+}
+
+void Gallery::showTabToolbarWidget() {
+ if (!m_toolbarTabDialog) {
+ m_toolbarTabDialog = new QToolbarTabDialog;
+ connect(m_toolbarTabDialog, SIGNAL(accepted()), this, SLOT(tabToolbarWidgetAccepted()));
+ connect(m_toolbarTabDialog, SIGNAL(rejected()), this, SLOT(tabToolbarWidgetRejected()));
+
+ QSearchField *searchField = new QSearchField(0);
+ m_toolbarTabDialog->addTab(searchField, QPixmap( ":/user-home.png" ), "Home", "Go Home");
+
+ QButton *b1 = new QButton(0, QButton::Recessed);
+ b1->setText("You've reached the trash");
+ m_toolbarTabDialog->addTab(b1, QPixmap( ":/user-trash.png" ), "Trash", "Trash it. Try me.");
+
+ QPushButton *b2 = new QPushButton(0);
+ b2->setText("Search is futile");
+ m_toolbarTabDialog->addTab(b2, QPixmap( ":/bookmarks.png" ), "Bookmarks", "Look for some bookmarks");
+
+ QTextEdit* textEdit = new QTextEdit;
+ textEdit->setText("This is some text!");
+ m_toolbarTabDialog->addTab(textEdit, QPixmap( ":/bookmarks.png" ), "Text", "Some text editing eh?");
+
+ QGroupBox* groupBox = new QGroupBox;
+ groupBox->setLayout(new QVBoxLayout);
+ groupBox->layout()->addWidget(new QLineEdit());
+ QListWidget* lw = new QListWidget;
+ for (int i = 0; i < 30; ++i) {
+ lw->addItem("1213123123 12 21 2 2 " + i);
+ }
+ groupBox->layout()->addWidget(lw);
+ m_toolbarTabDialog->addTab(groupBox, QPixmap( ":/user-home.png" ), "List", "Some lists!" );
+
+ QLineEdit* lineEdit = new QLineEdit;
+ m_toolbarTabDialog->addTab(lineEdit, QPixmap(":/user-home.png"), "LineEdit", "eh?");
+
+ QLineEdit* lineEdit2 = new QLineEdit;
+ m_toolbarTabDialog->addTab(lineEdit2, QPixmap(":/user-home.png"), "LineEdit2", "eh?");
+
+ m_toolbarTabDialog->setCurrentIndex(0);
+ }
+
+ m_toolbarTabDialog->show();
+}
+
+void Gallery::tabToolbarWidgetAccepted() {
+ m_toolbarTabDialog->deleteLater();
+ m_toolbarTabDialog = 0;
+}
+
+void Gallery::tabToolbarWidgetRejected() {
+// delete m_toolbarTabDialog;
+// m_toolbarTabDialog = 0;
}
View
10 gallery.h
@@ -3,12 +3,22 @@
#include <QWidget>
+class QToolbarTabDialog;
+
class Gallery : public QWidget
{
Q_OBJECT
public:
explicit Gallery(QWidget *parent = 0);
+
+public slots:
+ void showTabToolbarWidget();
+ void tabToolbarWidgetAccepted();
+ void tabToolbarWidgetRejected();
+
+private:
+ QToolbarTabDialog* m_toolbarTabDialog;
};
#endif // WIDGET_H
View
4 qocoa_mac.h
@@ -29,7 +29,7 @@ static inline NSString* fromQString(const QString &string)
{
const QByteArray utf8 = string.toUtf8();
const char* cString = utf8.constData();
- return [[NSString alloc] initWithUTF8String:cString];
+ return [[[NSString alloc] initWithUTF8String:cString] autorelease];
}
static inline QString toQString(NSString *string)
@@ -42,7 +42,7 @@ static inline QString toQString(NSString *string)
static inline NSImage* fromQPixmap(const QPixmap &pixmap)
{
CGImageRef cgImage = pixmap.toMacCGImageRef();
- return [[NSImage alloc] initWithCGImage:cgImage size:NSZeroSize];
+ return [[[NSImage alloc] initWithCGImage:cgImage size:NSZeroSize] autorelease];
}
static inline void setupLayout(void *cocoaView, QWidget *parent)
View
2  qsearchfield_mac.mm
@@ -209,7 +209,7 @@ -(BOOL)performKeyEquivalent:(NSEvent*)event {
return toQString([[pimpl->nsSearchField cell] placeholderString]);
}
-void QSearchField::setFocus(Qt::FocusReason reason)
+void QSearchField::setFocus(Qt::FocusReason)
{
Q_ASSERT(pimpl);
if (!pimpl)
View
68 qtoolbartabdialog.h
@@ -0,0 +1,68 @@
+/*
+ Copyright (C) 2012 by Leo Franchi <lfranchi@kde.org>
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ */
+
+#ifndef QTOOLBARTABWIDGET_H
+#define QTOOLBARTABWIDGET_H
+
+#include <QObject>
+#include <QScopedPointer>
+#include <QPixmap>
+
+class QToolbarTabDialogPrivate;
+
+class QAction;
+
+/**
+ Dialog with a toolbar that behaves like a tab widget.
+
+ Note that on OS X there are no OK/Cancel buttons, every setting should be applied immediately.
+ The accepted() signal will be emitted on close/hide regardless.
+ */
+class QToolbarTabDialog : public QObject
+{
+ Q_OBJECT
+public:
+ QToolbarTabDialog();
+ virtual ~QToolbarTabDialog();
+
+ /*
+ * If the given widget has a QSizePolicy of Fixed in either direction, the dialog will not be resizable in that
+ * direction.
+ */
+ void addTab(QWidget* page, const QPixmap& icon, const QString& label, const QString& tooltip = QString());
+
+ void setCurrentIndex(int index);
+
+ void show();
+ void hide();
+
+Q_SIGNALS:
+ void accepted();
+ void rejected();
+
+private:
+ QScopedPointer<QToolbarTabDialogPrivate> pimpl;
+
+ friend class ::QToolbarTabDialogPrivate;
+};
+
+#endif // QTOOLBARTABWIDGET_H
View
463 qtoolbartabdialog_mac.mm
@@ -0,0 +1,463 @@
+/*
+ Copyright (C) 2012 by Leo Franchi <lfranchi@kde.org>
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ */
+
+#include "qtoolbartabdialog.h"
+
+#include "qocoa_mac.h"
+
+#import <Foundation/NSAutoreleasePool.h>
+#import <AppKit/NSToolbar.h>
+#include <objc/objc-class.h>
+
+#include <QApplication>
+#include <QIcon>
+#include <QMap>
+#include <QUuid>
+#include <QMacNativeWidget>
+#include <QPointer>
+
+struct ItemData {
+ QPixmap icon;
+ QString text, tooltip;
+ QMacNativeWidget* nativeWidget;
+ QWidget* page;
+};
+
+
+namespace {
+
+static const int TOOLBAR_ITEM_WIDTH = 32;
+
+CGFloat ToolbarHeightForWindow(NSWindow *window)
+{
+ CGFloat toolbarHeight = 0.0f;
+
+ NSToolbar *toolbar = [window toolbar];
+
+ if(toolbar && [toolbar isVisible])
+ {
+ NSRect windowFrame = [NSWindow contentRectForFrameRect:[window frame]
+ styleMask:[window styleMask]];
+ toolbarHeight = NSHeight(windowFrame) - NSHeight([[window contentView] frame]);
+ }
+
+ return toolbarHeight;
+}
+
+};
+
+@interface ToolbarDelegate : NSObject<NSToolbarDelegate, NSWindowDelegate>
+{
+ QToolbarTabDialogPrivate *pimpl;
+}
+// Internal
+-(void)setPrivate:(QToolbarTabDialogPrivate*)withPimpl;
+
+// NSToolbarItem action
+-(void)changePanes:(id)sender;
+
+// NSToolbarDelegate
+-(NSToolbarItem *) toolbar: (NSToolbar *)toolbar itemForItemIdentifier: (NSString *) itemIdent willBeInsertedIntoToolbar:(BOOL) willBeInserted;
+-(NSArray*) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar;
+-(NSArray*) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar;
+-(NSArray*) toolbarSelectableItemIdentifiers: (NSToolbar*)toolbar;
+
+// NSWindowDelegate
+-(NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize;
+-(void)windowWillClose:(NSNotification *)notification;
+@end
+
+class QToolbarTabDialogPrivate {
+public:
+ QToolbarTabDialogPrivate(QToolbarTabDialog* dialog) : q(dialog),
+ currentPane(NULL),
+ minimumWidth(0)
+ {
+ }
+
+ ~QToolbarTabDialogPrivate() {
+ // unset the delegate and toolbar from the window and manually release them
+ // otherwise, for some reason the old delegate is laying around when we
+ // create a new NSWindow
+ [[prefsWindow toolbar] setDelegate:NULL];
+ [prefsWindow setToolbar:NULL];
+ [prefsWindow release];
+ [toolBar release];
+ [toolBarDelegate release];
+ }
+
+ void calculateSize() {
+ NSRect windowFrame = [prefsWindow frame];
+
+ while ([[toolBar visibleItems] count] < [[toolBar items] count]) {
+ //Each toolbar item is 32x32; we expand by one toolbar item width repeatedly until they all fit
+ windowFrame.origin.x -= TOOLBAR_ITEM_WIDTH / 2;
+ windowFrame.size.width += TOOLBAR_ITEM_WIDTH / 2;
+
+ [prefsWindow setFrame:windowFrame display:NO];
+ [prefsWindow setMinSize: windowFrame.size];
+ }
+ minimumWidth = windowFrame.size.width;
+ }
+
+ void showPaneWithIdentifier(NSString* ident) {
+ NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
+
+ const QString identStr = toQString(ident);
+ Q_ASSERT(items.contains(identStr));
+ if (!items.contains(identStr))
+ return;
+
+ QWidget* newWidget = items[identStr].nativeWidget;
+ Q_ASSERT(newWidget);
+ if (!newWidget)
+ return;
+
+ QWidget* newPage = items[identStr].page;
+ Q_ASSERT(newPage);
+ if (!newPage)
+ return;
+
+ const NSRect oldFrame = [[prefsWindow contentView] frame];
+
+ // Clear first responder on window and set a temporary NSView on the window
+ // while we change the widget out underneath
+ [prefsWindow makeFirstResponder:nil];
+
+ NSView *tempView = [[NSView alloc] initWithFrame:[[prefsWindow contentView] frame]];
+ [prefsWindow setContentView:tempView];
+ [tempView release];
+
+ QSize sizeToUse = newPage->sizeHint().isNull() ? newPage->size() : newPage->sizeHint();
+ sizeToUse.setWidth(qMax(sizeToUse.width(), newPage->minimumWidth()));
+ sizeToUse.setHeight(qMax(sizeToUse.height(), newPage->minimumHeight()));
+
+ static const int spacing = 4;
+
+ [prefsWindow setMinSize:NSMakeSize(sizeToUse.width(), sizeToUse.height())];
+
+ // Make room for the new view
+ NSRect newFrame = [prefsWindow frame];
+ newFrame.size.height = sizeToUse.height() + ([prefsWindow frame].size.height - [[prefsWindow contentView] frame].size.height) + spacing;
+ newFrame.size.width = sizeToUse.width() + spacing;
+
+ // Don't resize the width---only the height, so use the maximum width for any page
+ // or the same width as before, if the user already resized it to be larger.
+ newFrame.size.width < minimumWidth ? newFrame.size.width = minimumWidth
+ : newFrame.size.width = qMax(newFrame.size.width, oldFrame.size.width);
+
+ // Preserve upper left point of window during resize.
+ newFrame.origin.y += ([[prefsWindow contentView] frame].size.height - sizeToUse.height()) - spacing;
+
+ [prefsWindow setFrame:newFrame display:YES animate:YES];
+
+ [prefsWindow setContentView: [panes objectForKey:ident]];
+ currentPane = ident;
+
+ // Resize the Qt widget immediately as well
+ resizeCurrentPageToSize([[prefsWindow contentView] frame].size);
+
+ NSSize minSize = [prefsWindow frame].size;
+ minSize.height -= ToolbarHeightForWindow(prefsWindow);
+
+ [prefsWindow setMinSize:minSize];
+
+ BOOL canResize = YES;
+ NSSize maxSize = NSMakeSize(FLT_MAX, FLT_MAX);
+
+ if (newPage->sizePolicy().horizontalPolicy() == QSizePolicy::Fixed) {
+ canResize = NO;
+ maxSize.width = minSize.width;
+ }
+ if (newPage->sizePolicy().verticalPolicy() == QSizePolicy::Fixed) {
+ canResize = NO;
+ maxSize.height = minSize.height;
+ }
+
+
+ [prefsWindow setMaxSize:maxSize];
+ [prefsWindow setShowsResizeIndicator:canResize];
+
+ [prefsWindow setTitle:ident];
+ [prefsWindow makeFirstResponder:[panes objectForKey:ident]];
+
+ [pool drain];
+ }
+
+ void resizeCurrentPageToSize(NSSize frameSize) {
+ const QString curPane = toQString(currentPane);
+ if (items.contains(curPane) && items[curPane].nativeWidget) {
+ items[curPane].nativeWidget->resize(frameSize.width, frameSize.height);
+ }
+ }
+
+ void emitAccepted() {
+ if (q.isNull())
+ return;
+
+ q.data()->accepted();
+ }
+
+ QWeakPointer<QToolbarTabDialog> q;
+
+ NSWindow* prefsWindow;
+ ToolbarDelegate *toolBarDelegate;
+ QMap<QString, ItemData> items;
+
+ NSMutableDictionary *panes;
+ NSToolbar *toolBar;
+ NSString* currentPane;
+
+ int minimumWidth;
+};
+
+
+@implementation ToolbarDelegate
+
+-(id) init {
+ if( self = [super init] )
+ {
+ pimpl = nil;
+ }
+
+ return self;
+}
+
+-(void) setPrivate:(QToolbarTabDialogPrivate *)withPimpl
+{
+ pimpl = withPimpl;
+}
+
+-(void)changePanes:(id)sender
+{
+ Q_UNUSED(sender);
+ if (!pimpl)
+ return;
+
+ pimpl->showPaneWithIdentifier([pimpl->toolBar selectedItemIdentifier]);
+}
+
+-(NSToolbarItem *) toolbar: (NSToolbar *)toolbar itemForItemIdentifier: (NSString *) itemIdent willBeInsertedIntoToolbar:(BOOL) willBeInserted
+{
+ Q_UNUSED(toolbar);
+ Q_UNUSED(willBeInserted);
+ if (!pimpl)
+ return nil;
+
+ NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: itemIdent] autorelease];
+ const QString identQStr = toQString(itemIdent);
+ if (pimpl->items.contains(identQStr))
+ {
+ const ItemData& data = pimpl->items[identQStr];
+ NSString* label = fromQString(data.text);
+
+ [toolbarItem setLabel:label];
+ [toolbarItem setPaletteLabel:label];
+
+ [toolbarItem setToolTip:fromQString(data.tooltip)];
+ [toolbarItem setImage:fromQPixmap(data.icon)];
+
+ [toolbarItem setTarget: self];
+ [toolbarItem setAction: @selector(changePanes:)];
+
+ } else {
+ toolbarItem = nil;
+ }
+
+ return toolbarItem;
+}
+
+-(NSArray*) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
+{
+ Q_UNUSED(toolbar);
+ if (!pimpl)
+ return [NSArray array];
+
+ NSMutableArray* allowedItems = [[[NSMutableArray alloc] init] autorelease];
+
+ Q_FOREACH( const QString& identQStr, pimpl->items.keys())
+ [allowedItems addObject:fromQString(identQStr)];
+
+ [allowedItems addObjectsFromArray:[NSArray arrayWithObjects:NSToolbarSeparatorItemIdentifier,
+ NSToolbarSpaceItemIdentifier, NSToolbarFlexibleSpaceItemIdentifier,
+ NSToolbarCustomizeToolbarItemIdentifier, nil] ];
+
+ return allowedItems;
+}
+
+
+-(NSArray*) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
+{
+ Q_UNUSED(toolbar);
+ if (!pimpl)
+ return [NSArray array];
+
+ return [[[NSMutableArray alloc] initWithArray:[pimpl->panes allKeys]] autorelease];
+
+}
+
+
+-(NSArray*) toolbarSelectableItemIdentifiers: (NSToolbar*)toolbar
+{
+ Q_UNUSED(toolbar);
+ if (!pimpl)
+ return [NSArray array];
+
+ return [[[NSMutableArray alloc] initWithArray:[pimpl->panes allKeys]] autorelease];
+}
+
+-(NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
+{
+ if (!pimpl)
+ return frameSize;
+
+ pimpl->resizeCurrentPageToSize([[sender contentView] frame].size);
+
+ return frameSize;
+}
+
+-(void)windowWillClose:(NSNotification *)notification
+{
+ Q_UNUSED(notification);
+
+ pimpl->emitAccepted();
+}
+
+@end
+
+QToolbarTabDialog::QToolbarTabDialog() :
+ QObject(0),
+ pimpl(new QToolbarTabDialogPrivate(this))
+{
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+
+ pimpl->panes = [[NSMutableDictionary alloc] init];
+
+ static const int defaultWidth = 350;
+ static const int defaultHeight = 200;
+ pimpl->prefsWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, defaultWidth, defaultHeight)
+ styleMask:NSClosableWindowMask | NSResizableWindowMask | NSTitledWindowMask
+ backing:NSBackingStoreBuffered
+ defer:NO];
+
+ [pimpl->prefsWindow setReleasedWhenClosed:NO];
+ [pimpl->prefsWindow setTitle:@"Preferences"];
+
+ // identifier is some app-unique string, since all toolbars in an app share state. make this unique to this app's preferences window
+ pimpl->toolBar = [[NSToolbar alloc] initWithIdentifier:[NSString stringWithFormat:@"%@.prefspanel.toolbar", fromQString(QCoreApplication::instance()->applicationName())]];
+ [pimpl->toolBar setAllowsUserCustomization: NO];
+ [pimpl->toolBar setAutosavesConfiguration: NO];
+ [pimpl->toolBar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
+
+ pimpl->toolBarDelegate = [[ToolbarDelegate alloc] init];
+ [pimpl->toolBarDelegate setPrivate:pimpl.data()];
+
+ [pimpl->prefsWindow setDelegate:pimpl->toolBarDelegate];
+ [pimpl->toolBar setDelegate:pimpl->toolBarDelegate];
+
+ [pimpl->prefsWindow setToolbar:pimpl->toolBar];
+
+ [pool drain];
+}
+
+QToolbarTabDialog::~QToolbarTabDialog()
+{
+}
+
+void QToolbarTabDialog::addTab(QWidget* page, const QPixmap& icon, const QString& label, const QString& tooltip)
+{
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+
+ NSString* identifier = fromQString(label);
+
+ QMacNativeWidget* nativeWidget = new QMacNativeWidget;
+ nativeWidget->move(0, 0);
+ nativeWidget->setPalette(page->palette());
+ nativeWidget->setAutoFillBackground(true);
+
+ QVBoxLayout* l = new QVBoxLayout;
+ l->setContentsMargins(2, 2, 2, 2);
+ l->setSpacing(0);
+ page->setAttribute(Qt::WA_LayoutUsesWidgetRect);
+ page->setAttribute(Qt::WA_PaintOnScreen);
+ l->addWidget(page);
+ nativeWidget->setLayout(l);
+
+ NSView *nativeView = reinterpret_cast<NSView*>(nativeWidget->winId());
+ [nativeView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
+ [nativeView setAutoresizesSubviews:YES];
+
+ pimpl->minimumWidth = qMax(pimpl->minimumWidth, page->sizeHint().width());
+
+ nativeWidget->show();
+
+ ItemData data;
+ data.icon = icon;
+ data.text = label;
+ data.tooltip = tooltip;
+ data.nativeWidget = nativeWidget;
+ data.page = page;
+ pimpl->items.insert(label, data);
+
+ [pimpl->panes setObject:nativeView forKey:identifier];
+
+ pimpl->showPaneWithIdentifier(identifier);
+
+ [pimpl->toolBar insertItemWithItemIdentifier:identifier atIndex:[[pimpl->toolBar items] count]];
+ [pimpl->toolBar setSelectedItemIdentifier:identifier];
+ [[pimpl->prefsWindow standardWindowButton:NSWindowZoomButton] setEnabled:NO];
+
+ pimpl->calculateSize();
+ [pool drain];
+}
+
+
+void QToolbarTabDialog::setCurrentIndex(int index)
+{
+ Q_ASSERT(pimpl);
+ if (!pimpl)
+ return;
+
+ [pimpl->toolBar setSelectedItemIdentifier:[[[pimpl->toolBar items] objectAtIndex:index] itemIdentifier]];
+ pimpl->showPaneWithIdentifier([[[pimpl->toolBar items] objectAtIndex:index] itemIdentifier]);
+}
+
+void QToolbarTabDialog::show()
+{
+ Q_ASSERT(pimpl);
+ if (!pimpl)
+ return;
+
+ [pimpl->prefsWindow center];
+ [pimpl->prefsWindow makeKeyAndOrderFront:nil];
+}
+
+void QToolbarTabDialog::hide()
+{
+ Q_ASSERT(pimpl);
+ if (!pimpl)
+ return;
+
+ [pimpl->prefsWindow close];
+ emit accepted();
+}
+
View
167 qtoolbartabdialog_nonmac.cpp
@@ -0,0 +1,167 @@
+#include "qtoolbartabdialog.h"
+
+#include <QToolBar>
+#include <QStackedWidget>
+#include <QAction>
+#include <QVBoxLayout>
+#include <QDialog>
+#include <QDialogButtonBox>
+#include <QDebug>
+
+class QToolbarTabDialogPrivate : public QObject {
+ Q_OBJECT
+public:
+ QToolbarTabDialogPrivate(QToolbarTabDialog* qq) : q(qq), layout(0), toolbar(0), rightSpacer(0), stack(0), separator(0), buttons(0), actionGroup(0) {}
+
+public slots:
+ void actionTriggered(QAction* action) {
+ if (dialog.isNull())
+ return;
+
+ const int idx = toolbar->actions().indexOf(action);
+ Q_ASSERT(idx > -1);
+ if (idx < 0)
+ return;
+
+ stack->setCurrentIndex(idx);
+ }
+
+ void accepted() {
+ Q_ASSERT(!dialog.isNull());
+ Q_ASSERT(!q.isNull());
+
+ dialog.data()->hide();
+ emit q.data()->accepted();
+ }
+
+ void rejected() {
+ Q_ASSERT(!dialog.isNull());
+ Q_ASSERT(!q.isNull());
+
+ dialog.data()->hide();
+ emit q.data()->rejected();
+ }
+
+public:
+ QWeakPointer<QDialog> dialog;
+ QWeakPointer<QToolbarTabDialog> q;
+
+ QVBoxLayout* layout;
+ QToolBar* toolbar;
+ QAction* rightSpacer;
+ QStackedWidget* stack;
+ QFrame* separator;
+ QDialogButtonBox* buttons;
+ QActionGroup* actionGroup;
+
+};
+
+QToolbarTabDialog::QToolbarTabDialog() :
+ QObject(0),
+ pimpl(new QToolbarTabDialogPrivate(this))
+{
+ pimpl->dialog = new QDialog;
+ pimpl->dialog.data()->setModal(true);
+
+ pimpl->toolbar = new QToolBar(pimpl->dialog.data());
+ pimpl->toolbar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
+
+ pimpl->stack = new QStackedWidget(pimpl->dialog.data());
+
+ pimpl->separator = new QFrame(pimpl->dialog.data());
+ pimpl->separator->setFrameShape(QFrame::HLine);
+ pimpl->separator->setFrameShadow(QFrame::Sunken);
+
+ pimpl->actionGroup = new QActionGroup(pimpl->dialog.data());
+
+ connect(pimpl->toolbar, SIGNAL(actionTriggered(QAction*)), pimpl.data(), SLOT(actionTriggered(QAction*)));
+
+ pimpl->buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, pimpl->dialog.data());
+ connect(pimpl->buttons, SIGNAL(accepted()), pimpl->dialog.data(), SLOT(accept()));
+ connect(pimpl->buttons, SIGNAL(rejected()), pimpl->dialog.data(), SLOT(reject()));
+
+ connect(pimpl->dialog.data(), SIGNAL(accepted()), pimpl.data(), SLOT(accepted()));
+ connect(pimpl->dialog.data(), SIGNAL(rejected()), pimpl.data(), SLOT(rejected()));
+
+ QWidget* leftSpacer = new QWidget(pimpl->toolbar);
+ leftSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
+ QWidget* rightSpacer = new QWidget(pimpl->toolbar);
+ rightSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
+
+ pimpl->toolbar->addWidget(leftSpacer);
+ pimpl->rightSpacer = pimpl->toolbar->addWidget(rightSpacer);
+
+ pimpl->layout = new QVBoxLayout;
+ pimpl->layout->addWidget(pimpl->toolbar);
+ pimpl->layout->addWidget(pimpl->separator);
+ pimpl->layout->addWidget(pimpl->stack);
+ pimpl->layout->addWidget(pimpl->buttons);
+ pimpl->dialog.data()->setLayout(pimpl->layout);
+}
+
+QToolbarTabDialog::~QToolbarTabDialog()
+{
+ if (pimpl && !pimpl->dialog.isNull()) {
+ delete pimpl->dialog.data();
+ }
+}
+
+void QToolbarTabDialog::addTab(QWidget* page, const QPixmap& icon, const QString& label, const QString& tooltip)
+{
+ Q_ASSERT(pimpl);
+ if (!pimpl)
+ return;
+
+ pimpl->toolbar->removeAction(pimpl->rightSpacer);
+
+ QAction* action = new QAction(icon, label, pimpl->toolbar);
+ action->setCheckable(true);
+ action->setToolTip(tooltip);
+
+ pimpl->actionGroup->addAction(action);
+
+ pimpl->toolbar->addAction(action);
+ pimpl->stack->addWidget(page);
+
+ pimpl->toolbar->addAction(pimpl->rightSpacer);
+}
+
+void QToolbarTabDialog::setCurrentIndex(int index)
+{
+ Q_ASSERT(pimpl);
+ if (!pimpl || pimpl->dialog.isNull())
+ return;
+
+
+ Q_ASSERT(index < pimpl->toolbar->actions().length());
+ Q_ASSERT(index < pimpl->stack->count());
+ if (index < 0 || index > pimpl->toolbar->actions().length())
+ return;
+ if (index > pimpl->stack->count())
+ return;
+
+ if (pimpl->stack->currentIndex() != index)
+ pimpl->stack->setCurrentIndex(index);
+}
+
+void QToolbarTabDialog::show()
+{
+ Q_ASSERT(pimpl);
+ Q_ASSERT(!pimpl->dialog.isNull());
+ if (!pimpl || pimpl->dialog.isNull())
+ return;
+
+ pimpl->dialog.data()->show();
+}
+
+void QToolbarTabDialog::hide()
+{
+ Q_ASSERT(pimpl);
+ Q_ASSERT(!pimpl->dialog.isNull());
+ if (!pimpl || pimpl->dialog.isNull())
+ return;
+
+ pimpl->dialog.data()->hide();
+}
+
+#include "moc_qtoolbartabdialog_nonmac.cpp"
View
7 resources.qrc
@@ -0,0 +1,7 @@
+<RCC>
+ <qresource prefix="/">
+ <file>user-home.png</file>
+ <file>user-trash.png</file>
+ <file>bookmarks.png</file>
+ </qresource>
+</RCC>
View
BIN  user-home.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  user-trash.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Please sign in to comment.
Something went wrong with that request. Please try again.