Refactored: Moved all code which works with tab widget to custom tab widget class

This commit is contained in:
Igor Malinovskiy
2013-12-16 15:03:47 +02:00
parent d1d299100d
commit ca9e1eef7a
5 changed files with 200 additions and 153 deletions
+7 -1
View File
@@ -381,7 +381,7 @@ aa|bb Either aa or bb.
</item>
</layout>
</widget>
<widget class="QTabWidget" name="tabWidget">
<widget class="MainTabsWidget" name="tabWidget">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>1</horstretch>
@@ -591,6 +591,12 @@ aa|bb Either aa or bb.
<extends>QTreeView</extends>
<header>ConnectionTreeView.h</header>
</customwidget>
<customwidget>
<class>MainTabsWidget</class>
<extends>QTabWidget</extends>
<header>MainTabsWidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources>
<include location="../Resources/demo.qrc"/>
+1 -23
View File
@@ -1,5 +1,4 @@
#ifndef DEMO_H
#define DEMO_H
#pragma once
#include <QtWidgets/QMainWindow>
#include <QElapsedTimer>
@@ -28,35 +27,18 @@ private:
QMenu * keyMenu;
QMenu * connectionsMenu;
// TODO: move to custom tabWidget class
void addTab(QString&, QWidget*, QString icon = QString(), bool forceOpenInNewTab = false);
// todo: move to custom tabWidget class
void closeCurrentTabWithValue();
// todo: move to custom tabWidget class
void closeAllServerTabs(RedisServerItem *);
// todo: move to custom tabWidget class
void openKeyTab(RedisKeyItem * key, bool inNewTab = false);
// todo: move to custom tabWidget class
/** @return >=0 if exist **/
int getTabIndex(QString&);
// todo: move to custom Settings class
QString getConfigPath(const QString&);
void initFormButtons();
void initConnectionsTreeView();
//todo combine in one method - initContextMenus()
void initServerMenu();
void initKeyMenu();
void initConnectionsMenu();
// end todo
void initTabs();
void initUpdater();
void initFilter();
@@ -64,7 +46,6 @@ private:
void OnAddConnectionClick();
void OnConnectionTreeClick(const QModelIndex & index);
void OnConnectionTreeWheelClick(const QModelIndex & index);
void OnTabClose(int index);
void OnTreeViewContextMenu(const QPoint &);
void OnReloadServerInTree();
void OnDisconnectFromServer();
@@ -81,7 +62,4 @@ private:
void OnUIUnlock();
void OnStatusMessage(QString);
void OnKeyOpenInNewTab();
void OnKeyDeleted(QWidget * tab, RedisKeyItem * key);
};
#endif // DEMO_H
@@ -0,0 +1,39 @@
#pragma once
#include <QTabWidget>
class RedisServerItem;
class RedisKeyItem;
class MainTabsWidget : public QTabWidget
{
Q_OBJECT
public:
MainTabsWidget(QWidget * parent = nullptr);
int addTab(QString&, QWidget*, QString icon = QString(), bool forceOpenInNewTab = false);
int addTab(QWidget*, const QIcon&, const QString &);
int addTab(QWidget*, const QString &);
void closeTab(unsigned int index);
void closeAllServerTabs(RedisServerItem *);
void openKeyTab(RedisKeyItem * key, bool inNewTab = false);
protected:
void closeCurrentTabWithValue();
/** @return >=0 if exist **/
int getTabIndex(QString&);
protected slots:
void OnTabClose(int i);
void OnKeyDeleted(QWidget * tab, RedisKeyItem * key);
};
+6 -129
View File
@@ -27,7 +27,6 @@ MainWin::MainWin(QWidget *parent)
initKeyMenu();
initConnectionsMenu();
initFormButtons();
initTabs();
initUpdater();
initFilter();
@@ -97,17 +96,6 @@ void MainWin::initFormButtons()
connect(ui.pbImportConnections, SIGNAL(clicked()), SLOT(OnImportConnectionsClick()));
}
void MainWin::initTabs()
{
connect(ui.tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(OnTabClose(int)));
#ifndef Q_OS_DARWIN
//hide close button for first tab
// on Mac Os this code crash application
ui.tabWidget->tabBar()->tabButton(0, QTabBar::RightSide)->setFixedWidth(0);
#endif
}
void MainWin::initUpdater()
{
//set current version
@@ -205,106 +193,20 @@ void MainWin::OnConnectionTreeClick(const QModelIndex & index)
break;
case RedisKeyItem::TYPE:
openKeyTab((RedisKeyItem *)item);
ui.tabWidget->openKeyTab((RedisKeyItem *)item);
break;
}
}
void MainWin::openKeyTab(RedisKeyItem * key, bool inNewTab)
{
QWidget * viewTab = new ValueTab(key);
connect(viewTab, SIGNAL(keyDeleted(QWidget *, RedisKeyItem *)),
this, SLOT(OnKeyDeleted(QWidget *, RedisKeyItem *)));
connect(viewTab, SIGNAL(error(const QString &)),
this, SLOT(OnError(const QString &)));
QString keyFullName = key->getTabLabelText();
if (inNewTab) {
addTab(keyFullName, viewTab, QString(), true);
} else {
addTab(keyFullName, viewTab);
}
}
void MainWin::OnConnectionTreeWheelClick(const QModelIndex & index)
{
QStandardItem * item = connections->itemFromIndex(index);
if (item->type() == RedisKeyItem::TYPE) {
openKeyTab((RedisKeyItem *)item, true);
ui.tabWidget->openKeyTab((RedisKeyItem *)item, true);
}
}
void MainWin::OnTabClose(int index)
{
ValueTab * w = qobject_cast<ValueTab *> (ui.tabWidget->widget(index));
ui.tabWidget->removeTab(index);
w->close();
}
int MainWin::getTabIndex(QString& name)
{
for (int i = 0; i < ui.tabWidget->count(); ++i)
{
if (name == ui.tabWidget->tabText(i)) {
return i;
}
}
return -1;
}
void MainWin::closeCurrentTabWithValue()
{
int currIndex = ui.tabWidget->currentIndex();
if (currIndex == -1)
return;
QWidget * w = ui.tabWidget->widget(currIndex);
if (w->objectName() == "valueTabReady") {
OnTabClose(currIndex);
}
}
void MainWin::closeAllServerTabs(RedisServerItem * server)
{
unsigned int tabsCount = ui.tabWidget->count();
QString title;
for (int tabIndex = 0; tabIndex < tabsCount; tabIndex++)
{
title = ui.tabWidget->widget(tabIndex)->windowTitle();
//if (title.contains))
}
}
void MainWin::addTab(QString& tabName, QWidget* tab, QString icon, bool forceOpenInNewTab)
{
int currIndex;
if (!forceOpenInNewTab) {
closeCurrentTabWithValue();
}
if (icon.isEmpty()) {
currIndex = ui.tabWidget->addTab(tab, tabName);
} else {
currIndex = ui.tabWidget->addTab(tab, QIcon(icon), tabName);
}
ui.tabWidget->setCurrentIndex(currIndex);
}
// todo: move responsibility to ConnectionTreeView
// ConnectionTreeView::setItemsContextMenu( QHash( int => QMenu ) )
@@ -472,7 +374,7 @@ void MainWin::OnServerInfoOpen()
serverInfoViewTab * tab = new serverInfoViewTab(server->text(), info);
QString serverName = QString("Info: %1").arg(server->text());
addTab(serverName, tab, ":/images/serverinfo.png");
ui.tabWidget->addTab(serverName, tab, ":/images/serverinfo.png");
}
void MainWin::OnConsoleOpen()
@@ -489,7 +391,7 @@ void MainWin::OnConsoleOpen()
QString serverName = server->text();
addTab(serverName, tab, ":/images/terminal.png");
ui.tabWidget->addTab(serverName, tab, ":/images/terminal.png");
}
void MainWin::OnKeyOpenInNewTab()
@@ -499,7 +401,7 @@ void MainWin::OnKeyOpenInNewTab()
if (item == nullptr || item->type() != RedisKeyItem::TYPE)
return;
openKeyTab((RedisKeyItem *)item, true);
ui.tabWidget->openKeyTab((RedisKeyItem *)item, true);
}
void MainWin::OnError(QString msg)
@@ -520,29 +422,4 @@ void MainWin::OnUIUnlock()
void MainWin::OnStatusMessage(QString message)
{
statusBar()->showMessage(message);
}
// TODO: move responsibility to ValueTab + RedisServerDbItem
void MainWin::OnKeyDeleted(QWidget * tab, RedisKeyItem * key)
{
if (tab == nullptr || key == nullptr)
return;
int widgetsCount = ui.tabWidget->count();
int widgetIndex = -1;
for (int currentWidget = 0; currentWidget < widgetsCount; currentWidget++)
{
if (tab == ui.tabWidget->widget(currentWidget)) {
widgetIndex = currentWidget;
break;
}
}
if (widgetIndex == -1) {
return;
}
OnTabClose(widgetIndex);
}
}
@@ -0,0 +1,147 @@
#include "MainTabsWidget.h"
#include <QTabBar>
#include "valueViewTab.h"
MainTabsWidget::MainTabsWidget(QWidget * parent)
: QTabWidget(parent)
{
connect(this, SIGNAL(tabCloseRequested(int)), this, SLOT(OnTabClose(int)));
}
int MainTabsWidget::addTab(QWidget*w, const QIcon&i, const QString &label)
{
#ifndef Q_OS_DARWIN
//hide close button for first tab
// on Mac Os this code crash application
if (count() == 0) {
int currentIndex = QTabWidget::addTab(w, i, label);
tabBar()->tabButton(0, QTabBar::RightSide)->setFixedWidth(0);
return currentIndex;
}
#endif
return QTabWidget::addTab(w, i, label);
}
int MainTabsWidget::addTab(QWidget*w, const QString &label)
{
return QTabWidget::addTab(w, label);
}
int MainTabsWidget::addTab(QString& tabName, QWidget* tab, QString icon, bool forceOpenInNewTab)
{
int currIndex;
if (!forceOpenInNewTab) {
closeCurrentTabWithValue();
}
if (icon.isEmpty()) {
currIndex = QTabWidget::addTab(tab, tabName);
} else {
currIndex = QTabWidget::addTab(tab, QIcon(icon), tabName);
}
setCurrentIndex(currIndex);
return currIndex;
}
void MainTabsWidget::openKeyTab(RedisKeyItem * key, bool inNewTab)
{
QWidget * viewTab = new ValueTab(key);
connect(viewTab, SIGNAL(keyDeleted(QWidget *, RedisKeyItem *)),
this, SLOT(OnKeyDeleted(QWidget *, RedisKeyItem *)));
connect(viewTab, SIGNAL(error(const QString &)),
this, SLOT(OnError(const QString &)));
QString keyFullName = key->getTabLabelText();
if (inNewTab) {
addTab(keyFullName, viewTab, QString(), true);
} else {
addTab(keyFullName, viewTab);
}
}
void MainTabsWidget::OnKeyDeleted(QWidget * tab, RedisKeyItem * key)
{
if (tab == nullptr || key == nullptr)
return;
int widgetsCount = count();
int widgetIndex = -1;
for (int currentWidget = 0; currentWidget < widgetsCount; currentWidget++)
{
if (tab == widget(currentWidget)) {
widgetIndex = currentWidget;
break;
}
}
if (widgetIndex == -1) {
return;
}
closeTab(widgetIndex);
}
int MainTabsWidget::getTabIndex(QString& name)
{
for (int i = 0; i < count(); ++i)
{
if (name == tabText(i)) {
return i;
}
}
return -1;
}
void MainTabsWidget::closeCurrentTabWithValue()
{
int currIndex = currentIndex();
if (currIndex == -1)
return;
QWidget * w = widget(currIndex);
if (w->objectName() == "valueTabReady") {
closeTab(currIndex);
}
}
void MainTabsWidget::closeTab(unsigned int index)
{
ValueTab * w = qobject_cast<ValueTab *> (widget(index));
removeTab(index);
w->close();
}
void MainTabsWidget::OnTabClose(int index)
{
closeTab((unsigned int)index);
}
void MainTabsWidget::closeAllServerTabs(RedisServerItem * server)
{
unsigned int tabsCount = count();
QString title;
for (int tabIndex = 0; tabIndex < tabsCount; tabIndex++)
{
title = widget(tabIndex)->windowTitle();
//if (title.contains))
//todo implement this
}
}