Rabbit Remote Control 0.0.35
Loading...
Searching...
No Matches
ViewTable.cpp
1// Author: Kang Lin <kl222@126.com>
2
3#include <QResizeEvent>
4#include <QTabBar>
5#include <QStyleOption>
6#include <QDebug>
7#include <QScrollBar>
8#include <QFileInfo>
9#include <QDir>
10#include <QLoggingCategory>
11#include "mainwindow.h"
12#include "ViewTable.h"
13
14static Q_LOGGING_CATEGORY(log, "App.View.Table")
15static Q_LOGGING_CATEGORY(logRecord, "App.View.Table.Record")
16
17CViewTable::CViewTable(CParameterApp *pPara, QWidget *parent)
18 : CView(pPara, parent),
19 m_pTab(nullptr)
20{
21 qDebug(log) << Q_FUNC_INFO << this;
22 bool check = false;
23 setFocusPolicy(Qt::NoFocus);
24
25 m_pTab = new QTabWidget(this);
26 m_pTab->setTabsClosable(true);
27 m_pTab->setUsesScrollButtons(true);
28 m_pTab->setMovable(true);
29 m_pTab->setFocusPolicy(Qt::NoFocus);
30
31 if(m_pParameterApp)
32 {
33 m_pTab->setTabPosition(m_pParameterApp->GetTabPosition());
34 check = connect(m_pParameterApp, SIGNAL(sigTabPositionChanged()),
35 this, SLOT(slotTabPositionChanged()));
36 Q_ASSERT(check);
37 }
38
39 check = connect(m_pTab, SIGNAL(tabCloseRequested(int)),
40 this, SLOT(slotTabCloseRequested(int)));
41 Q_ASSERT(check);
42 check = connect(m_pTab, SIGNAL(currentChanged(int)),
43 this, SLOT(slotCurrentChanged(int)));
44 Q_ASSERT(check);
45
46 m_pTab->tabBar()->setContextMenuPolicy(Qt::CustomContextMenu);
47 check = connect(m_pTab->tabBar(), &QTabBar::customContextMenuRequested,
48 this, [&](const QPoint& pos){
49 QPoint p = pos;
50 p = m_pTab->tabBar()->mapToGlobal(pos);
51 emit customContextMenuRequested(p);
52 });
53
54 Q_ASSERT(check);
55}
56
57CViewTable::~CViewTable()
58{
59 qDebug(log) << Q_FUNC_INFO << this;
60 if(m_pTab) {
61 m_pTab->clear();
62 delete m_pTab;
63 }
64}
65
66void CViewTable::slotCurrentChanged(int index)
67{
68 qDebug(log) << "CViewTable::slotCurrentChanged";
69 emit sigCurrentChanged(GetViewer(index));
70}
71
72void CViewTable::slotTabCloseRequested(int index)
73{
74 QWidget* pView = GetViewer(index);
75 if(!pView) return;
76 emit sigCloseView(pView);
77}
78
79void CViewTable::slotTabPositionChanged()
80{
81 if(!m_pParameterApp)
82 return;
83 m_pTab->setTabPosition(m_pParameterApp->GetTabPosition());
84}
85
86int CViewTable::AddView(QWidget *pView)
87{
88 int nIndex = -1;
89 if(!pView)
90 {
91 qCritical(log) << "CViewTable::AddView: The pView is nullptr";
92 return -1;
93 }
94 //qDebug(log) << "CViewTable::AddView: Window title:" << pView->windowTitle();
95 nIndex = m_pTab->indexOf(pView);
96 if(-1 == nIndex) {
97 nIndex = m_pTab->addTab(pView, pView->windowTitle());
98 }
99 m_pTab->setCurrentIndex(nIndex);
100
101 return 0;
102}
103
104int CViewTable::RemoveView(QWidget *pView)
105{
106 int nIndex = GetViewIndex(pView);
107 if(-1 == nIndex) return 0;
108 // Note: The following order cannot be changed
109 m_pTab->removeTab(nIndex);
110 return 0;
111}
112
113void CViewTable::SetWidowsTitle(QWidget* pView, const QString& szTitle,
114 const QIcon &icon, const QString &szToolTip)
115{
116 if(!pView) {
117 qCritical(log) << "CViewTable::SetWidowsTitle: The pView is nullptr";
118 return;
119 }
120 //qDebug(log) << "CViewTable::SetWidowsTitle: Window title:" << szTitle;
121 pView->setWindowTitle(szTitle);
122 int nIndex = GetViewIndex(pView);
123 m_pTab->setTabText(nIndex, szTitle);
124 if(m_pParameterApp->GetEnableTabToolTip())
125 m_pTab->setTabToolTip(nIndex, szToolTip);
126 else
127 m_pTab->setTabToolTip(nIndex, "");
128 if(m_pParameterApp->GetEnableTabIcon())
129 m_pTab->setTabIcon(nIndex, icon);
130 else
131 m_pTab->setTabIcon(nIndex, QIcon());
132}
133
134int CViewTable::SetFullScreen(bool bFull)
135{
136 if(!m_pTab) return -1;
137 if(bFull)
138 {
139 m_szStyleSheet = m_pTab->styleSheet();
140 SetVisibleTab(false);
141 //qDebug(log) << m_szStyleSheet;
142 m_pTab->setStyleSheet("QTabWidget::pane{top:0px;left:0px;border:none;}");
143 m_pTab->showFullScreen();
144 } else {
145 SetVisibleTab(m_pParameterApp->GetTabBar());
146 m_pTab->setStyleSheet(m_szStyleSheet);
147 m_pTab->showNormal();
148 }
149 return 0;
150}
151
152int CViewTable::SetVisibleTab(bool bVisible)
153{
154 m_pTab->tabBar()->setVisible(bVisible);
155 return 0;
156}
157
158QWidget *CViewTable::GetViewer(int index)
159{
160 if(index < 0 || index >= m_pTab->count())
161 return nullptr;
162
163 return m_pTab->widget(index);
164}
165
166int CViewTable::GetViewIndex(QWidget *pView)
167{
168 for(int i = 0; i < m_pTab->count(); i++)
169 {
170 QWidget* p = GetViewer(i);
171 if(p == pView)
172 return i;
173 }
174 return -1;
175}
176
177// \note The return QWidget* must is same as CConnecter::GetViewer()
179{
180 QWidget* pView = m_pTab->currentWidget();
181 if(!pView) return pView;
182 return pView;
183}
184
185int CViewTable::SetCurrentView(QWidget* pView)
186{
187 int nIndex = m_pTab->indexOf(pView);
188 if(-1 != nIndex) {
189 m_pTab->setCurrentIndex(nIndex);
190 return 0;
191 }
192 return -1;
193}
194
195void CViewTable::resizeEvent(QResizeEvent *event)
196{
197 if(!m_pTab)
198 return;
199 m_pTab->move(0, 0);
200 m_pTab->resize(event->size());
201}
The CViewTable class.
Definition ViewTable.h:16
virtual int RemoveView(QWidget *pView) override
virtual QWidget * GetCurrentView() override
virtual int AddView(QWidget *pView) override
Definition ViewTable.cpp:86
The CView class.
Definition View.h:25
void sigCloseView(const QWidget *pView)