Rabbit Remote Control 0.0.30
Loading...
Searching...
No Matches
FavoriteView.cpp
1#include "FavoriteView.h"
2
3#include <QMouseEvent>
4#include <QDrag>
5#include <QMimeData>
6#include <QApplication>
7#include <QHeaderView>
8#include <QSettings>
9#include <QStandardItemModel>
10#include <QStandardItem>
11#include <QSettings>
12#include <QMenu>
13#include <QAction>
14#include <QInputDialog>
15#include <QMessageBox>
16#include <QDir>
17#include <QLoggingCategory>
18
19#include "FavoriteMimeData.h"
20#include "RabbitCommonDir.h"
21
22static Q_LOGGING_CATEGORY(log, "App.MainWindow.Favorite")
23
24CFavoriteView::CFavoriteView(QWidget *parent) : QTreeView(parent),
25 m_pDockTitleBar(nullptr),
26 m_pModel(nullptr)
27{
28 setWindowTitle(tr("Favorite"));
29 setFocusPolicy(Qt::NoFocus);
30 header()->hide();
31
32 setAcceptDrops(true);
33
34 m_pModel = new QStandardItemModel(this);
35 setModel(m_pModel);
36
37 setContextMenuPolicy(Qt::CustomContextMenu);
38 bool check = connect(this,
39 SIGNAL(customContextMenuRequested(const QPoint &)),
40 this, SLOT(slotCustomContextMenu(const QPoint &)));
41 Q_ASSERT(check);
42 check = connect(this, SIGNAL(clicked(const QModelIndex&)),
43 this, SLOT(slotFavrtieClicked(const QModelIndex&)));
44 Q_ASSERT(check);
45 check = connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
46 this, SLOT(slotFavortiedoubleClicked(const QModelIndex&)));
47 Q_ASSERT(check);
48
49 m_szSaveFile = RabbitCommon::CDir::Instance()->GetDirUserConfig()
50 + QDir::separator() + "Favorite.ini";
51 QSettings set(m_szSaveFile, QSettings::IniFormat);
52 int nRootCount = 0;
53 nRootCount = set.value("RootCount").toInt();
54 for(int i = 0; i < nRootCount; i++)
55 {
56 QString name = set.value("Name_" + QString::number(i)).toString();
57 QString file = set.value("File_" + QString::number(i)).toString();
58 if(name.isEmpty()) {
59 qCritical(log) << "Current node is empty!";
60 continue;
61 }
62 QStandardItem* item = new QStandardItem(name);
63 item->setIcon(QIcon::fromTheme("network-wired"));
64 if(item)
65 {
66 item->setData(file);
67 m_pModel->appendRow(item);
68 }
69 }
70 int nGroupCount = set.value("GroupCount").toInt();
71 for(int g = 0; g < nGroupCount; g++)
72 {
73 QString szGroup = set.value("Group_" + QString::number(g)).toString();
74 QStandardItem* pGroup = new QStandardItem(szGroup);
75 pGroup->setIcon(QIcon::fromTheme("network-workgroup"));
76 m_pModel->appendRow(pGroup);
77 int n = set.value(szGroup + "/" + "count").toInt();
78 for(int i = 0; i < n; i++)
79 {
80 QString name = set.value(szGroup + "/Name_" + QString::number(i)).toString();
81 QString file = set.value(szGroup + "/File_" + QString::number(i)).toString();
82 if(name.isEmpty())
83 {
84 qCritical(log) << "Current node is empty!";
85 continue;
86 }
87 QStandardItem* item = new QStandardItem(name);
88 item->setIcon(QIcon::fromTheme("network-wired"));
89 if(item)
90 {
91 item->setData(file);
92 pGroup->appendRow(item);
93 }
94 }
95 }
96
97 m_pDockTitleBar = new RabbitCommon::CTitleBar(parent);
98 // Create tools pushbutton in title bar
99 m_pMenu = new QMenu(tr("Tools"), m_pDockTitleBar);
100 check = connect(m_pMenu, SIGNAL(aboutToShow()), this, SLOT(slotMenu()));
101 Q_ASSERT(check);
102 QPushButton* pTools = m_pDockTitleBar->CreateSmallPushButton(
103 QIcon::fromTheme("tools"), m_pDockTitleBar);
104 pTools->setToolTip(tr("Tools"));
105 pTools->setMenu(m_pMenu);
106 QList<QWidget*> lstWidget;
107 lstWidget << pTools;
108 m_pDockTitleBar->AddWidgets(lstWidget);
109}
110
111CFavoriteView::~CFavoriteView()
112{
113 Save();
114}
115
116int CFavoriteView::Save()
117{
118 int nRet = 0;
119 if(!m_pModel)
120 return -1;
121 QSettings set(m_szSaveFile, QSettings::IniFormat);
122 int nRootCount = 0;
123 int nGroup = 0;
124 for(int rootIndex = 0; rootIndex < m_pModel->rowCount(); rootIndex++)
125 {
126 auto rootItem = m_pModel->item(rootIndex);
127 if(!rootItem) continue;
128 QString text = rootItem->text();
129 QVariant data = rootItem->data();
130 if(rootItem->hasChildren() || rootItem->data().isNull())
131 {
132 int nCount = 0;
133 QString szGroup = rootItem->text();
134 set.setValue("Group_" + QString::number(nGroup++), szGroup);
135 for(int childIndex = 0; childIndex < rootItem->rowCount(); childIndex++)
136 {
137 auto childItem = rootItem->child(childIndex);
138 if(!childItem) continue;
139 set.setValue(szGroup + "/Name_" + QString::number(nCount), childItem->text());
140 set.setValue(szGroup + "/File_" + QString::number(nCount), childItem->data());
141 nCount++;
142 }
143 set.setValue(szGroup + "/" + "count", nCount);
144 } else {
145 set.setValue("Name_" + QString::number(nRootCount), rootItem->text());
146 set.setValue("File_" + QString::number(nRootCount), rootItem->data());
147 nRootCount++;
148 }
149 }
150 set.setValue("RootCount", nRootCount);
151 set.setValue("GroupCount", nGroup);
152 return nRet;
153}
154
155int CFavoriteView::AddFavorite(const QString& szName, const QString &szFile)
156{
157 if(!m_pModel) return -1;
158 QStandardItem* pItem = nullptr;
159 QString szGroup;
160 auto indexes = selectionModel()->selectedIndexes();
161 if(indexes.isEmpty())
162 {
163 auto it = m_pModel->findItems(szName, Qt::MatchFixedString);
164 if(it.isEmpty())
165 {
166 pItem = new QStandardItem(szName);
167 pItem->setIcon(QIcon::fromTheme("network-wired"));
168 m_pModel->appendRow(pItem);
169 } else {
170 QList<QStandardItem*>::iterator i;
171 for(i = it.begin(); i != it.end(); i++)
172 {
173 if((*i)->data() == szFile)
174 break;
175 }
176 if(it.end() != i)
177 return 1;
178 pItem = new QStandardItem(szName);
179 pItem->setIcon(QIcon::fromTheme("network-wired"));
180 m_pModel->appendRow(pItem);
181 }
182 } else {
183 auto itemGroup = m_pModel->itemFromIndex(indexes[0]);
184 if(itemGroup->data().isValid())
185 {
186 auto it = m_pModel->findItems(szName, Qt::MatchFixedString);
187 if(it.isEmpty())
188 {
189 pItem = new QStandardItem(szName);
190 pItem->setIcon(QIcon::fromTheme("network-wired"));
191 m_pModel->appendRow(pItem);
192 }
193 } else {
194 szGroup = itemGroup->text();
195 auto lstGroup = m_pModel->findItems(szGroup, Qt::MatchFixedString);
196 if(lstGroup.isEmpty())
197 {
198 QStandardItem* pGroup = new QStandardItem(szGroup);
199 m_pModel->appendRow(pGroup);
200 pItem = new QStandardItem(szName);
201 pItem->setIcon(QIcon::fromTheme("network-wired"));
202 pGroup->appendRow(pItem);
203 } else {
204 if(lstGroup[0]->data().isValid()) return 2;
205 for(int i = 0; i < lstGroup[0]->rowCount(); i++)
206 {
207 auto item = lstGroup[0]->child(i);
208 if(item->text() == szName)
209 return -3;
210 }
211 pItem = new QStandardItem(szName);
212 pItem->setIcon(QIcon::fromTheme("network-wired"));
213 lstGroup[0]->appendRow(pItem);
214 }
215 }
216 }
217 if(pItem)
218 pItem->setData(szFile);
219 return 0;
220}
221
222void CFavoriteView::slotFavrtieClicked(const QModelIndex &index)
223{
224}
225
226void CFavoriteView::slotFavortiedoubleClicked(const QModelIndex &index)
227{
228 auto item = m_pModel->itemFromIndex(index);
229 if(!item) return;
230 if(editTriggers() != QTreeView::NoEditTriggers)
231 return;
232 QString szFile = item->data().toString();
233 if(!szFile.isEmpty())
234 emit sigConnect(szFile, false);
235}
236
237void CFavoriteView::slotDoubleEditNode(bool bEdit)
238{
239 if(bEdit)
240 setEditTriggers(QTreeView::DoubleClicked);
241 else
242 setEditTriggers(QTreeView::NoEditTriggers);
243}
244
245void CFavoriteView::slotMenu()
246{
247 auto index = this->currentIndex();
248 auto item = m_pModel->itemFromIndex(index);
249 m_pMenu->clear();
250 if(item)
251 {
252 if(item->data().isValid()) {
253 m_pMenu->addAction(tr("Connect"), this, SLOT(slotConnect()));
254 m_pMenu->addAction(tr("Open settings and connect"), this, SLOT(slotOpenConnect()));
255 m_pMenu->addAction(tr("Delete Connect"), this, SLOT(slotDelete()));
256 }
257 m_pMenu->addSeparator();
258 m_pMenu->addAction(tr("New group"), this, SLOT(slotNewGroup()));
259 if(!item->data().isValid())
260 m_pMenu->addAction(tr("Delete group"), this, SLOT(slotDelete()));
261 } else
262 m_pMenu->addAction(tr("New group"), this, SLOT(slotNewGroup()));
263
264 m_pMenu->addSeparator();
265 m_pMenu->addAction(tr("Add to favorite"), this, SIGNAL(sigFavorite()));
266}
267
268void CFavoriteView::slotCustomContextMenu(const QPoint &pos)
269{
270 slotMenu();
271 m_pMenu->exec(mapToGlobal(pos));
272}
273
274void CFavoriteView::slotConnect()
275{
276 auto lstIndex = selectionModel()->selectedIndexes();
277 foreach(auto index, lstIndex)
278 {
279 slotFavortiedoubleClicked(index);
280 }
281}
282
283void CFavoriteView::slotOpenConnect()
284{
285 auto lstIndex = selectionModel()->selectedIndexes();
286 foreach(auto index, lstIndex)
287 {
288 auto item = m_pModel->itemFromIndex(index);
289 QString szFile = item->data().toString();
290 if(!szFile.isEmpty())
291 emit sigConnect(szFile, true);
292 }
293}
294
295void CFavoriteView::slotDelete()
296{
297 auto lstIndex = selectionModel()->selectedIndexes();
298 foreach(auto index, lstIndex)
299 m_pModel->removeRow(index.row(), index.parent());
300}
301
302void CFavoriteView::slotNewGroup()
303{
304 QString szGroup = QInputDialog::getText(this, tr("Input"), tr("Input group name"));
305 if(szGroup.isEmpty()) return;
306 auto lstItem = m_pModel->findItems(szGroup);
307 if(!lstItem.isEmpty())
308 {
309 QMessageBox::critical(this, tr("Error"), tr("The group [%1] is existed").arg(szGroup));
310 return;
311 }
312
313 QStandardItem* pGroup = new QStandardItem(szGroup);
314 pGroup->setIcon(QIcon::fromTheme("network-workgroup"));
315 m_pModel->appendRow(pGroup);
316}
317
318void CFavoriteView::dragEnterEvent(QDragEnterEvent *event)
319{
320 qDebug(log) << "dragEnterEvent";
321 const CFavoriteMimeData* pData = qobject_cast<const CFavoriteMimeData*>(event->mimeData());
322 if (pData)
323 {
324 qDebug(log) << "dragEnterEvent acceptProposedAction";
325 event->acceptProposedAction();
326 }
327}
328
329void CFavoriteView::dragMoveEvent(QDragMoveEvent *event)
330{
331}
332
333void CFavoriteView::dropEvent(QDropEvent *event)
334{
335 qDebug(log) << "dropEvent";
336 const CFavoriteMimeData *pData = qobject_cast<const CFavoriteMimeData*>(event->mimeData());
337 if(!pData) return;
338 QStandardItemModel* pModel = dynamic_cast<QStandardItemModel*>(model());
339 if(!pModel) return;
340#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
341 auto index = indexAt(event->position().toPoint());
342#else
343 auto index = indexAt(event->pos());
344#endif
345 if(index.isValid())
346 {
347 auto item = pModel->itemFromIndex(index);
348 if(item->data().isNull())
349 {
350 foreach(auto i, pData->m_Items)
351 {
352 qDebug(log) << "dropEvent:" << item->text();
353
354 auto newItem = NewItem(i);
355 item->appendRow(newItem);
356 if(event->dropAction() == Qt::MoveAction)
357 pModel->removeRow(i.row(), i.parent());
358 }
359 } else
360 qWarning(log) << "Don't group node. the data:" << item->data();
361 }else{
362 foreach(auto i, pData->m_Items)
363 {
364 pModel->appendRow(NewItem(i));
365 if(event->dropAction() == Qt::MoveAction)
366 pModel->removeRow(i.row(), i.parent());
367 }
368 }
369
370 event->accept();
371}
372
373QStandardItem* CFavoriteView::NewItem(const QModelIndex &index)
374{
375 QStandardItemModel* pModel = dynamic_cast<QStandardItemModel*>(model());
376 if(!pModel) return nullptr;
377 auto item = pModel->itemFromIndex(index);
378 if(!item) return nullptr;
379 auto ri = new QStandardItem(item->text());
380 ri->setData(item->data());
381 return ri;
382}
383
384void CFavoriteView::mousePressEvent(QMouseEvent *event)
385{
386 if (event->button() == Qt::LeftButton)
387 m_DragStartPosition = event->pos();
388 QTreeView::mousePressEvent(event);
389}
390
391void CFavoriteView::mouseMoveEvent(QMouseEvent *event)
392{
393 qDebug(log) << "mouseMoveEvent";
394 do{
395 if (!(event->buttons() & Qt::LeftButton))
396 break;
397 if ((event->pos() - m_DragStartPosition).manhattanLength()
398 < QApplication::startDragDistance())
399 break;
400 qDebug(log) << "mouseMoveEvent drag";
401 QDrag *drag = new QDrag(this);
403 pData->m_Items = this->selectionModel()->selectedIndexes();
404 drag->setMimeData(pData);
405
406 Qt::DropAction dropAction = Qt::MoveAction;
407 if(event->modifiers() & Qt::ControlModifier)
408 dropAction = Qt::CopyAction;
409 drag->exec(dropAction);
410 } while (false);
411
412 QTreeView::mouseMoveEvent(event);
413}