From fc2e1c7fd7b5533a4efcd3b798bb2eead54b0f41 Mon Sep 17 00:00:00 2001 From: Peter Gunnarsson Date: Wed, 3 Aug 2011 10:02:51 +0200 Subject: [PATCH] Add new widgets with simpler code than what is used currently (FancyTabWidget). --- .../src/libs/utils/mylistwidget.cpp | 38 ++++++ .../src/libs/utils/mylistwidget.h | 53 +++++++++ .../src/libs/utils/mytabbedstackwidget.cpp | 109 ++++++++++++++++++ .../src/libs/utils/mytabbedstackwidget.h | 74 ++++++++++++ .../src/libs/utils/mytabwidget.cpp | 48 ++++++++ .../openpilotgcs/src/libs/utils/mytabwidget.h | 55 +++++++++ ground/openpilotgcs/src/libs/utils/utils.pro | 10 +- 7 files changed, 385 insertions(+), 2 deletions(-) create mode 100644 ground/openpilotgcs/src/libs/utils/mylistwidget.cpp create mode 100644 ground/openpilotgcs/src/libs/utils/mylistwidget.h create mode 100644 ground/openpilotgcs/src/libs/utils/mytabbedstackwidget.cpp create mode 100644 ground/openpilotgcs/src/libs/utils/mytabbedstackwidget.h create mode 100644 ground/openpilotgcs/src/libs/utils/mytabwidget.cpp create mode 100644 ground/openpilotgcs/src/libs/utils/mytabwidget.h diff --git a/ground/openpilotgcs/src/libs/utils/mylistwidget.cpp b/ground/openpilotgcs/src/libs/utils/mylistwidget.cpp new file mode 100644 index 000000000..f5fbfb639 --- /dev/null +++ b/ground/openpilotgcs/src/libs/utils/mylistwidget.cpp @@ -0,0 +1,38 @@ +/** + ****************************************************************************** + * + * @file mylistwidget.cpp + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009. + * @brief + * @see The GNU Public License (GPL) Version 3 + * @defgroup + * @{ + * + *****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include "mylistwidget.h" + +QStyleOptionViewItem MyListWidget::viewOptions() const +{ + QStyleOptionViewItem option = QListWidget::viewOptions(); + if (m_iconAbove) { + option.decorationPosition = QStyleOptionViewItem::Top; + option.displayAlignment = Qt::AlignCenter; + } + return option; +} diff --git a/ground/openpilotgcs/src/libs/utils/mylistwidget.h b/ground/openpilotgcs/src/libs/utils/mylistwidget.h new file mode 100644 index 000000000..40059927e --- /dev/null +++ b/ground/openpilotgcs/src/libs/utils/mylistwidget.h @@ -0,0 +1,53 @@ +/** + ****************************************************************************** + * + * @file mylistwidget.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009. + * @brief + * @see The GNU Public License (GPL) Version 3 + * @defgroup + * @{ + * + *****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef MYLISTWIDGET_H +#define MYLISTWIDGET_H + +#include "utils_global.h" + +#include + +/* + * MyListWidget is a plain QListWidget but with the added option + * to place the icon above the label in ListMode. This is achieved + * the easiest by subclassing QListWidget and overriding viewOptions(). + */ +class QTCREATOR_UTILS_EXPORT MyListWidget : public QListWidget +{ + Q_OBJECT +public: + MyListWidget(QWidget *parent) : QListWidget(parent), m_iconAbove(false) { } + void setIconAbove(bool iconAbove) { m_iconAbove = iconAbove; } +protected: + QStyleOptionViewItem viewOptions() const; +private: + bool m_iconAbove; +}; + +#endif // MYLISTWIDGET_H diff --git a/ground/openpilotgcs/src/libs/utils/mytabbedstackwidget.cpp b/ground/openpilotgcs/src/libs/utils/mytabbedstackwidget.cpp new file mode 100644 index 000000000..faf2c911e --- /dev/null +++ b/ground/openpilotgcs/src/libs/utils/mytabbedstackwidget.cpp @@ -0,0 +1,109 @@ +/** + ****************************************************************************** + * + * @file mytabbedstackwidget.cpp + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009. + * @brief + * @see The GNU Public License (GPL) Version 3 + * @defgroup + * @{ + * + *****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include "mytabbedstackwidget.h" +#include +#include +#include +#include + +MyTabbedStackWidget::MyTabbedStackWidget(QWidget *parent, bool isVertical, bool iconAbove) + : QWidget(parent), + m_vertical(isVertical), + m_iconAbove(iconAbove) +{ + m_listWidget = new MyListWidget(this); + m_listWidget->setIconAbove(m_iconAbove); + m_stackWidget = new QStackedWidget(); + m_stackWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + + QBoxLayout *toplevelLayout; + if (m_vertical) { + toplevelLayout = new QHBoxLayout; + toplevelLayout->addWidget(m_listWidget); + toplevelLayout->addWidget(m_stackWidget); + m_listWidget->setFlow(QListView::TopToBottom); + m_listWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding); + } else { + toplevelLayout = new QVBoxLayout; + toplevelLayout->addWidget(m_stackWidget); + toplevelLayout->addWidget(m_listWidget); + m_listWidget->setFlow(QListView::LeftToRight); + m_listWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); + } + if (m_iconAbove && m_vertical) + m_listWidget->setFixedWidth(90); // this should be computed instead + + toplevelLayout->setSpacing(0); + toplevelLayout->setContentsMargins(0, 0, 0, 0); + m_listWidget->setSpacing(0); + m_listWidget->setContentsMargins(0, 0, 0, 0); + m_stackWidget->setContentsMargins(0, 0, 0, 0); + setLayout(toplevelLayout); + + connect(m_listWidget, SIGNAL(currentRowChanged(int)), this, SLOT(showWidget(int))); +} + +void MyTabbedStackWidget::insertTab(const int index, QWidget *tab, const QIcon &icon, const QString &label) +{ + tab->setContentsMargins(0, 0, 0, 0); + m_stackWidget->insertWidget(index, tab); + QListWidgetItem *item = new QListWidgetItem(icon, label); + item->setToolTip(label); + m_listWidget->insertItem(index, item); +} + +void MyTabbedStackWidget::removeTab(int index) +{ + m_stackWidget->removeWidget(m_stackWidget->widget(index)); + QListWidgetItem *item = m_listWidget->item(index); + m_listWidget->removeItemWidget(item); + delete item; +} + +int MyTabbedStackWidget::currentIndex() const +{ + return m_listWidget->currentRow(); +} + +void MyTabbedStackWidget::setCurrentIndex(int index) +{ + m_listWidget->setCurrentRow(index); +} + +void MyTabbedStackWidget::showWidget(int index) +{ + emit currentAboutToShow(index); + m_stackWidget->setCurrentIndex(index); + emit currentChanged(index); +} + +void MyTabbedStackWidget::insertCornerWidget(int index, QWidget *widget) +{ + widget->hide(); +} + diff --git a/ground/openpilotgcs/src/libs/utils/mytabbedstackwidget.h b/ground/openpilotgcs/src/libs/utils/mytabbedstackwidget.h new file mode 100644 index 000000000..428ed8646 --- /dev/null +++ b/ground/openpilotgcs/src/libs/utils/mytabbedstackwidget.h @@ -0,0 +1,74 @@ +/** + ****************************************************************************** + * + * @file mytabbedstackwidget.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009. + * @brief + * @see The GNU Public License (GPL) Version 3 + * @defgroup + * @{ + * + *****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef MYTABBEDSTACKWIDGET_H +#define MYTABBEDSTACKWIDGET_H + +#include "utils/mylistwidget.h" +#include + +/* + * MyTabbedStackWidget is a MyListWidget combined with a QStackedWidget, + * similar in function to QTabWidget. + */ +class QTCREATOR_UTILS_EXPORT MyTabbedStackWidget : public QWidget +{ + Q_OBJECT + +public: + MyTabbedStackWidget(QWidget *parent = 0, bool isVertical = false, bool iconAbove = true); + + void insertTab(int index, QWidget *tab, const QIcon &icon, const QString &label); + void removeTab(int index); + void setIconSize(int size) { m_listWidget->setIconSize(QSize(size, size)); } + + int currentIndex() const; + + void insertCornerWidget(int index, QWidget *widget); + int cornerWidgetCount() { return m_cornerWidgetCount; } + +signals: + void currentAboutToShow(int index); + void currentChanged(int index); + +public slots: + void setCurrentIndex(int index); + +private slots: + void showWidget(int index); + +private: + MyListWidget *m_listWidget; + QStackedWidget *m_stackWidget; + QWidget *m_selectionWidget; + bool m_vertical; + bool m_iconAbove; + int m_cornerWidgetCount; +}; + +#endif // MYTABBEDSTACKWIDGET_H diff --git a/ground/openpilotgcs/src/libs/utils/mytabwidget.cpp b/ground/openpilotgcs/src/libs/utils/mytabwidget.cpp new file mode 100644 index 000000000..2a1a7bee4 --- /dev/null +++ b/ground/openpilotgcs/src/libs/utils/mytabwidget.cpp @@ -0,0 +1,48 @@ +/** + ****************************************************************************** + * + * @file mytabwidget.cpp + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009. + * @brief + * @see The GNU Public License (GPL) Version 3 + * @defgroup + * @{ + * + *****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include "mytabwidget.h" +#include + +MyTabWidget::MyTabWidget(QWidget *parent) + : QTabWidget(parent) +{ + QTabBar *tabBar = QTabWidget::tabBar(); + connect(tabBar, SIGNAL(tabMoved(int, int)), this, SLOT(myTabMoved(int,int))); +} + +void MyTabWidget::moveTab(int from, int to) +{ + QTabBar *tabBar = QTabWidget::tabBar(); + tabBar->moveTab(from, to); +} + + +void MyTabWidget::myTabMoved(int from, int to) +{ + emit tabMoved(from, to); +} diff --git a/ground/openpilotgcs/src/libs/utils/mytabwidget.h b/ground/openpilotgcs/src/libs/utils/mytabwidget.h new file mode 100644 index 000000000..8f941110b --- /dev/null +++ b/ground/openpilotgcs/src/libs/utils/mytabwidget.h @@ -0,0 +1,55 @@ +/** + ****************************************************************************** + * + * @file mytabwidget.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009. + * @brief + * @see The GNU Public License (GPL) Version 3 + * @defgroup + * @{ + * + *****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#ifndef MYTABWIDGET_H +#define MYTABWIDGET_H + +#include "utils_global.h" + +#include + +/* + * MyTabWidget is a plain QTabWidget with the addition of the signal + * tabMoved(int, int) which QTabBar has but for some reason is + * not made available from QTabWidget. + */ +class QTCREATOR_UTILS_EXPORT MyTabWidget : public QTabWidget +{ + Q_OBJECT + +public: + MyTabWidget(QWidget *parent = 0); + void moveTab(int from, int to); + +private slots: + void myTabMoved(int from, int to); + +signals: + void tabMoved(int from, int to); +}; + +#endif // MYTABWIDGET_H diff --git a/ground/openpilotgcs/src/libs/utils/utils.pro b/ground/openpilotgcs/src/libs/utils/utils.pro index 7b923a567..804f7c813 100644 --- a/ground/openpilotgcs/src/libs/utils/utils.pro +++ b/ground/openpilotgcs/src/libs/utils/utils.pro @@ -45,7 +45,10 @@ SOURCES += reloadpromptutils.cpp \ coordinateconversions.cpp \ pathutils.cpp \ worldmagmodel.cpp \ - homelocationutil.cpp + homelocationutil.cpp \ + mytabbedstackwidget.cpp \ + mytabwidget.cpp \ + mylistwidget.cpp SOURCES += xmlconfig.cpp win32 { @@ -96,7 +99,10 @@ HEADERS += utils_global.h \ coordinateconversions.h \ pathutils.h \ worldmagmodel.h \ - homelocationutil.h + homelocationutil.h \ + mytabbedstackwidget.h \ + mytabwidget.h \ + mylistwidget.h HEADERS += xmlconfig.h FORMS += filewizardpage.ui \