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