Rabbit Remote Control 0.0.34
Loading...
Searching...
No Matches
ViewSplitter.cpp
1// Author: Kang Lin <kl222@126.com>
2
3#include "ViewSplitter.h"
4#include <QVBoxLayout>
5#include <QLoggingCategory>
6#include <QEvent>
7
8static Q_LOGGING_CATEGORY(log, "App.View.Splitter")
9
10CViewSplitter::CViewSplitter(QWidget *parent)
11 : CView(parent)
12 , m_nRow(0)
13 , m_nCount(0)
14 , m_nIdxRow(-1)
15 , m_nIdxCol(-1)
16{
17 qDebug(log) << Q_FUNC_INFO << this;
18
19 QVBoxLayout* p = new QVBoxLayout(this);
20 if(!p) {
21 qCritical(log) << "new QVBoxLayout fail";
22 return;
23 }
24 p->setContentsMargins(0, 0, 0, 0);
25 setLayout(p);
26 m_pMain = new QSplitter(Qt::Vertical, this);
27 if(!m_pMain) {
28 qCritical(log) << "m_pMain is nullptr";
29 return;
30 }
31 p->addWidget(m_pMain);
32 m_HandleWidth = m_pMain->handleWidth();
33 /*
34 setStyleSheet("QWidget { background-color: red }");
35 m_pMain->setStyleSheet("QSplitter::handle { background-color: white }");//*/
36}
37
38CViewSplitter::~CViewSplitter()
39{
40 qDebug(log) << Q_FUNC_INFO << this;
41 for(int i = 0; i < m_nRow; i++)
42 {
43 auto sp = m_Row[i];
44 for(int j = 0; j < sp->count(); j++)
45 {
46 auto pw = sp->widget(j);
47 pw->setParent(nullptr);
48 }
49 }
50}
51
52int CViewSplitter::AddView(QWidget *pView)
53{
54 int nRet = 0;
55 QSplitter* sp = nullptr;
56 if(!pView) {
57 qCritical(log) << "The view is null";
58 Q_ASSERT(pView);
59 return -1;
60 }
61
62 // 是否需要新增加一行
63 if(m_nCount + 1 > m_nRow * m_nRow) {
64 QSplitter* p = new QSplitter(Qt::Horizontal, m_pMain);
65 if(p) {
66 //p->setStyleSheet("QSplitter::handle { background-color: blue }");
67 m_Row.append(p);
68 m_nRow++;
69 }
70 }
71 // 顺序查找哪行没满,加到未尾
72 int i = 0;
73 for(i = 0; i < m_nRow; i++) {
74 auto p = m_Row[i];
75 if(p->count() < m_nRow) {
76 sp = p;
77 break;
78 }
79 }
80 if(!sp)
81 return nRet;
82 // 设置当前视频索引
83 m_nIdxRow = i;
84 m_nIdxCol = sp->count();
85 sp->addWidget(pView);
86 pView->show();
87 //sp->setStretchFactor(sp->count() - 1, 1);
88 m_nCount++;
89 qDebug(log) << Q_FUNC_INFO << "Row:" << m_nRow << "Count:" << m_nCount
90 << "Current row:" << m_nIdxRow << "Current col:" << m_nIdxCol
91 << "Current count:" << sp->count();
92 emit sigCurrentChanged(pView);
93 return nRet;
94}
95
96int CViewSplitter::RemoveView(QWidget *pView)
97{
98 int nRet = 0;
99 if(!pView)
100 return -1;
101 // Get view position
102 int nRow = -1;
103 int nCol = -1;
104 nRet = GetIndex(pView, nRow, nCol);
105 if(nRet)
106 return nRet;
107 QWidget* pCurView = GetCurrentView();
108 if(pView == pCurView)
109 {
110 pCurView = nullptr;
111 if(m_Row[nRow]->count() > nCol + 1)
112 pCurView = GetView(nRow, nCol + 1);
113 else if(m_Row[nRow]->count() - 1 == nCol && 0 != nCol)
114 pCurView = GetView(nRow, nCol -1);
115 else if(0 == nCol && nRow + 1 < m_nRow) {
116 for(int i = nRow + 1; i < m_nRow; i++) {
117 if(m_Row[i]->count() > 0) {
118 pCurView = GetView(nRow + 1, 0);
119 break;
120 }
121 }
122 }
123 if(!pCurView) {
124 for(int i = nRow - 1; i >= 0; i--) {
125 if(m_Row[i]->count() > 0) {
126 pCurView = GetView(i, m_Row[i]->count() - 1);
127 break;
128 }
129 }
130 }
131 }
132 // Delete view
133 pView->setParent(nullptr);
134 qDebug(log) << "Row:" << nRow << "remain:" << m_Row[nRow]->count();
135 m_nCount--;
136
137 qDebug(log) << "Row:" << m_nRow << "Count:" << m_nCount
138 << "Current row:" << m_nIdxRow << "Current col:" << m_nIdxCol;
139 if(pCurView)
140 pCurView->setFocus();
141
142 return SetCurrentView(pCurView);
143}
144
146{
147 return GetView(m_nIdxRow, m_nIdxCol);
148}
149
150int CViewSplitter::SetCurrentView(QWidget *pView)
151{
152 int nRet = 0;
153 if(!pView)
154 return -1;
155 int nRow = -1;
156 int nCol = -1;
157 nRet = GetIndex(pView, nRow, nCol);
158 if(nRet) return nRet;
159
160 if(m_nIdxCol == nCol && m_nIdxRow == nRow)
161 return 0;
162
163 m_nIdxRow = nRow;
164 m_nIdxCol = nCol;
165 pView->setFocus();
166 emit sigCurrentChanged(pView);
167
168 return nRet;
169}
170
171void CViewSplitter::SetWidowsTitle(
172 QWidget *pView, const QString &szTitle,
173 const QIcon &icon, const QString &szToolTip)
174{
175 if(!pView)
176 return;
177 // pView->setWindowTitle(szTitle);
178 // pView->setWindowIcon(icon);
179 // pView->setToolTip(szToolTip);
180}
181
182int CViewSplitter::SetFullScreen(bool bFull)
183{
184 int nRet = 0;
185 if(0 >= m_nCount) return 0;
186 QWidget* p = GetCurrentView();
187 qDebug(log) << "CurrentView:" << p;
188 if(!p) return 0;
189 if(bFull) {
190 m_szStyleSheet = styleSheet();
191 //qDebug(log) << "Style:" << m_szStyleSheet;
192 setStyleSheet("QWidget::pane{top:0px;left:0px;border:none;}");
193 m_HandleWidth = m_pMain->handleWidth();
194 m_pMain->setHandleWidth(0);
195 for(int i = 0; i < m_nRow; i++) {
196 auto sp = m_Row[i];
197 if(!sp) {
198 qCritical(log) << "Row" << i << "is null";
199 continue;
200 }
201 sp->setHandleWidth(0);
202 for(int j = 0; j < m_nRow; j++) {
203 if(m_nIdxRow == i && m_nIdxCol == j)
204 continue;
205 auto p = sp->widget(j);
206 if(p) {
207 p->hide();
208 } else {
209 qCritical(log) << "Widget: Row" << i << "Col" << j << "is null";
210 }
211 }
212 }
213 }
214 else {
215 setStyleSheet(m_szStyleSheet);
216 m_pMain->setHandleWidth(m_HandleWidth);
217 for(int i = 0; i < m_nRow; i++) {
218 auto sp = m_Row[i];
219 sp->setHandleWidth(m_HandleWidth);
220 if(!sp) continue;
221 for(int j = 0; j < m_nRow; j++) {
222 auto p = sp->widget(j);
223 if(p)
224 p->show();
225 }
226 }
227 }
228 return nRet;
229}
230
231void CViewSplitter::slotSystemCombination()
232{
233}
234
235int CViewSplitter::GetIndex(QWidget* pView, int &nRow, int &nCol)
236{
237 if(!pView)
238 return -1;
239
240 nCol = -1;
241
242 for(nRow = 0; nRow < m_nRow; nRow++)
243 {
244 auto sp = m_Row[nRow];
245 if(!sp) continue;
246 nCol = sp->indexOf(pView);
247 if(-1 < nCol)
248 return 0;
249 }
250
251 return -1;
252}
253
254QWidget* CViewSplitter::GetView(const int &nRow, const int &nCol)
255{
256 QWidget* p = nullptr;
257 QSplitter* sp = nullptr;
258 if(-1 < nRow && nRow < m_nRow)
259 sp = m_Row[nRow];
260 if(sp && -1 < nCol && nCol < sp->count())
261 p = sp->widget(nCol);
262 qDebug(log) << "GetView()" << "Row number:" << m_nRow << m_Row.size()
263 << "Count:" << m_nCount
264 << "row:" << m_nIdxRow << "col:" << m_nIdxCol;
265 return p;
266}
The split view class.
virtual int RemoveView(QWidget *pView) override
virtual QWidget * GetCurrentView() override
virtual int AddView(QWidget *pView) override
The CView class.
Definition View.h:24