Rabbit Remote Control 0.0.37
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 if(m_pParameterApp->GetTabElided())
34 m_pTab->setElideMode(Qt::ElideLeft);
35 m_pTab->setTabPosition(m_pParameterApp->GetTabPosition());
36 check = connect(m_pParameterApp, SIGNAL(sigTabPositionChanged()),
37 this, SLOT(slotTabPositionChanged()));
38 Q_ASSERT(check);
39 check = connect(m_pParameterApp, &CParameterApp::sigTabElided,
40 this, [&](){
41 if(m_pParameterApp->GetTabElided())
42 m_pTab->setElideMode(Qt::ElideLeft);
43 else
44 m_pTab->setElideMode(Qt::ElideNone);
45 });
46 }
47
48 check = connect(m_pTab, SIGNAL(tabCloseRequested(int)),
49 this, SLOT(slotTabCloseRequested(int)));
50 Q_ASSERT(check);
51 check = connect(m_pTab, SIGNAL(currentChanged(int)),
52 this, SLOT(slotCurrentChanged(int)));
53 Q_ASSERT(check);
54
55 m_pTab->tabBar()->setContextMenuPolicy(Qt::CustomContextMenu);
56 check = connect(m_pTab->tabBar(), &QTabBar::customContextMenuRequested,
57 this, [&](const QPoint& pos){
58 QPoint p = pos;
59 p = m_pTab->tabBar()->mapToGlobal(pos);
60 emit customContextMenuRequested(p);
61 });
62
63 Q_ASSERT(check);
64}
65
66CViewTable::~CViewTable()
67{
68 qDebug(log) << Q_FUNC_INFO << this;
69 if(m_pTab) {
70 m_pTab->clear();
71 delete m_pTab;
72 }
73}
74
75void CViewTable::slotCurrentChanged(int index)
76{
77 qDebug(log) << "CViewTable::slotCurrentChanged";
78 emit sigCurrentChanged(GetViewer(index));
79}
80
81void CViewTable::slotTabCloseRequested(int index)
82{
83 QWidget* pView = GetViewer(index);
84 if(!pView) return;
85 emit sigCloseView(pView);
86}
87
88void CViewTable::slotTabPositionChanged()
89{
90 if(!m_pParameterApp)
91 return;
92 m_pTab->setTabPosition(m_pParameterApp->GetTabPosition());
93}
94
95int CViewTable::AddView(QWidget *pView)
96{
97 int nIndex = -1;
98 if(!pView)
99 {
100 qCritical(log) << "CViewTable::AddView: The pView is nullptr";
101 return -1;
102 }
103 //qDebug(log) << "CViewTable::AddView: Window title:" << pView->windowTitle();
104 nIndex = m_pTab->indexOf(pView);
105 if(-1 == nIndex) {
106 nIndex = m_pTab->addTab(pView, pView->windowTitle());
107 }
108 m_pTab->setCurrentIndex(nIndex);
109
110 return 0;
111}
112
113int CViewTable::RemoveView(QWidget *pView)
114{
115 int nIndex = GetViewIndex(pView);
116 if(-1 == nIndex) return 0;
117 // Note: The following order cannot be changed
118 m_pTab->removeTab(nIndex);
119 return 0;
120}
121
122void CViewTable::SetWidowsTitle(QWidget* pView, const QString& szTitle,
123 const QIcon &icon, const QString &szToolTip)
124{
125 if(!pView) {
126 qCritical(log) << "CViewTable::SetWidowsTitle: The pView is nullptr";
127 return;
128 }
129 //qDebug(log) << "CViewTable::SetWidowsTitle: Window title:" << szTitle;
130 pView->setWindowTitle(szTitle);
131 int nIndex = GetViewIndex(pView);
132 m_pTab->setTabText(nIndex, szTitle);
133 if(m_pParameterApp->GetEnableTabToolTip())
134 m_pTab->setTabToolTip(nIndex, szToolTip);
135 else
136 m_pTab->setTabToolTip(nIndex, "");
137 if(m_pParameterApp->GetEnableTabIcon())
138 m_pTab->setTabIcon(nIndex, icon);
139 else
140 m_pTab->setTabIcon(nIndex, QIcon());
141}
142
143int CViewTable::SetFullScreen(bool bFull)
144{
145 if(!m_pTab) return -1;
146 if(bFull)
147 {
148 m_szStyleSheet = m_pTab->styleSheet();
149 SetVisibleTab(false);
150 //qDebug(log) << m_szStyleSheet;
151 m_pTab->setStyleSheet("QTabWidget::pane{top:0px;left:0px;border:none;}");
152 m_pTab->showFullScreen();
153 } else {
154 SetVisibleTab(m_pParameterApp->GetTabBar());
155 m_pTab->setStyleSheet(m_szStyleSheet);
156 m_pTab->showNormal();
157 }
158 return 0;
159}
160
161int CViewTable::SetVisibleTab(bool bVisible)
162{
163 m_pTab->tabBar()->setVisible(bVisible);
164 return 0;
165}
166
167QWidget *CViewTable::GetViewer(int index)
168{
169 if(index < 0 || index >= m_pTab->count())
170 return nullptr;
171
172 return m_pTab->widget(index);
173}
174
175int CViewTable::GetViewIndex(QWidget *pView)
176{
177 for(int i = 0; i < m_pTab->count(); i++)
178 {
179 QWidget* p = GetViewer(i);
180 if(p == pView)
181 return i;
182 }
183 return -1;
184}
185
186// \note The return QWidget* must is same as CConnecter::GetViewer()
188{
189 QWidget* pView = m_pTab->currentWidget();
190 if(!pView) return pView;
191 return pView;
192}
193
194int CViewTable::SetCurrentView(QWidget* pView)
195{
196 int nIndex = m_pTab->indexOf(pView);
197 if(-1 != nIndex) {
198 m_pTab->setCurrentIndex(nIndex);
199 return 0;
200 }
201 return -1;
202}
203
204void CViewTable::resizeEvent(QResizeEvent *event)
205{
206 if(!m_pTab)
207 return;
208 m_pTab->move(0, 0);
209 m_pTab->resize(event->size());
210}
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:95
The CView class.
Definition View.h:25
void sigCloseView(const QWidget *pView)