1#include "FavoriteView.h"
9#include <QStandardItemModel>
10#include <QStandardItem>
14#include <QInputDialog>
17#include <QLoggingCategory>
19#include "FavoriteMimeData.h"
20#include "RabbitCommonDir.h"
22static Q_LOGGING_CATEGORY(log,
"App.MainWindow.Favorite")
25 m_pDockTitleBar(
nullptr),
28 setWindowTitle(tr(
"Favorite"));
29 setFocusPolicy(Qt::NoFocus);
34 m_pModel =
new QStandardItemModel(
this);
37 setContextMenuPolicy(Qt::CustomContextMenu);
38 bool check = connect(
this,
39 SIGNAL(customContextMenuRequested(
const QPoint &)),
40 this, SLOT(slotCustomContextMenu(
const QPoint &)));
42 check = connect(
this, SIGNAL(clicked(
const QModelIndex&)),
43 this, SLOT(slotFavrtieClicked(
const QModelIndex&)));
45 check = connect(
this, SIGNAL(doubleClicked(
const QModelIndex&)),
46 this, SLOT(slotFavortiedoubleClicked(
const QModelIndex&)));
49 m_szSaveFile = RabbitCommon::CDir::Instance()->GetDirUserConfig()
50 + QDir::separator() +
"Favorite.ini";
51 QSettings set(m_szSaveFile, QSettings::IniFormat);
53 nRootCount = set.value(
"RootCount").toInt();
54 for(
int i = 0; i < nRootCount; i++)
56 QString name = set.value(
"Name_" + QString::number(i)).toString();
57 QString file = set.value(
"File_" + QString::number(i)).toString();
59 qCritical(log) <<
"Current node is empty!";
62 QStandardItem* item =
new QStandardItem(name);
63 item->setIcon(QIcon::fromTheme(
"network-wired"));
67 m_pModel->appendRow(item);
70 int nGroupCount = set.value(
"GroupCount").toInt();
71 for(
int g = 0; g < nGroupCount; g++)
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++)
80 QString name = set.value(szGroup +
"/Name_" + QString::number(i)).toString();
81 QString file = set.value(szGroup +
"/File_" + QString::number(i)).toString();
84 qCritical(log) <<
"Current node is empty!";
87 QStandardItem* item =
new QStandardItem(name);
88 item->setIcon(QIcon::fromTheme(
"network-wired"));
92 pGroup->appendRow(item);
97 m_pDockTitleBar =
new RabbitCommon::CTitleBar(parent);
99 m_pMenu =
new QMenu(tr(
"Tools"), m_pDockTitleBar);
100 check = connect(m_pMenu, SIGNAL(aboutToShow()),
this, SLOT(slotMenu()));
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;
108 m_pDockTitleBar->AddWidgets(lstWidget);
111CFavoriteView::~CFavoriteView()
116int CFavoriteView::Save()
121 QSettings set(m_szSaveFile, QSettings::IniFormat);
124 for(
int rootIndex = 0; rootIndex < m_pModel->rowCount(); rootIndex++)
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())
133 QString szGroup = rootItem->text();
134 set.setValue(
"Group_" + QString::number(nGroup++), szGroup);
135 for(
int childIndex = 0; childIndex < rootItem->rowCount(); childIndex++)
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());
143 set.setValue(szGroup +
"/" +
"count", nCount);
145 set.setValue(
"Name_" + QString::number(nRootCount), rootItem->text());
146 set.setValue(
"File_" + QString::number(nRootCount), rootItem->data());
150 set.setValue(
"RootCount", nRootCount);
151 set.setValue(
"GroupCount", nGroup);
155int CFavoriteView::AddFavorite(
const QString& szName,
const QString &szFile)
157 if(!m_pModel)
return -1;
158 QStandardItem* pItem =
nullptr;
160 auto indexes = selectionModel()->selectedIndexes();
161 if(indexes.isEmpty())
163 auto it = m_pModel->findItems(szName, Qt::MatchFixedString);
166 pItem =
new QStandardItem(szName);
167 pItem->setIcon(QIcon::fromTheme(
"network-wired"));
168 m_pModel->appendRow(pItem);
170 QList<QStandardItem*>::iterator i;
171 for(i = it.begin(); i != it.end(); i++)
173 if((*i)->data() == szFile)
178 pItem =
new QStandardItem(szName);
179 pItem->setIcon(QIcon::fromTheme(
"network-wired"));
180 m_pModel->appendRow(pItem);
183 auto itemGroup = m_pModel->itemFromIndex(indexes[0]);
184 if(itemGroup->data().isValid())
186 auto it = m_pModel->findItems(szName, Qt::MatchFixedString);
189 pItem =
new QStandardItem(szName);
190 pItem->setIcon(QIcon::fromTheme(
"network-wired"));
191 m_pModel->appendRow(pItem);
194 szGroup = itemGroup->text();
195 auto lstGroup = m_pModel->findItems(szGroup, Qt::MatchFixedString);
196 if(lstGroup.isEmpty())
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);
204 if(lstGroup[0]->data().isValid())
return 2;
205 for(
int i = 0; i < lstGroup[0]->rowCount(); i++)
207 auto item = lstGroup[0]->child(i);
208 if(item->text() == szName)
211 pItem =
new QStandardItem(szName);
212 pItem->setIcon(QIcon::fromTheme(
"network-wired"));
213 lstGroup[0]->appendRow(pItem);
218 pItem->setData(szFile);
222void CFavoriteView::slotFavrtieClicked(
const QModelIndex &index)
226void CFavoriteView::slotFavortiedoubleClicked(
const QModelIndex &index)
228 auto item = m_pModel->itemFromIndex(index);
230 if(editTriggers() != QTreeView::NoEditTriggers)
232 QString szFile = item->data().toString();
233 if(!szFile.isEmpty())
234 emit sigConnect(szFile,
false);
237void CFavoriteView::slotDoubleEditNode(
bool bEdit)
240 setEditTriggers(QTreeView::DoubleClicked);
242 setEditTriggers(QTreeView::NoEditTriggers);
245void CFavoriteView::slotMenu()
247 auto index = this->currentIndex();
248 auto item = m_pModel->itemFromIndex(index);
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()));
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()));
262 m_pMenu->addAction(tr(
"New group"),
this, SLOT(slotNewGroup()));
264 m_pMenu->addSeparator();
265 m_pMenu->addAction(tr(
"Add to favorite"),
this, SIGNAL(sigFavorite()));
268void CFavoriteView::slotCustomContextMenu(
const QPoint &pos)
271 m_pMenu->exec(mapToGlobal(pos));
274void CFavoriteView::slotConnect()
276 auto lstIndex = selectionModel()->selectedIndexes();
277 foreach(
auto index, lstIndex)
279 slotFavortiedoubleClicked(index);
283void CFavoriteView::slotOpenConnect()
285 auto lstIndex = selectionModel()->selectedIndexes();
286 foreach(
auto index, lstIndex)
288 auto item = m_pModel->itemFromIndex(index);
289 QString szFile = item->data().toString();
290 if(!szFile.isEmpty())
291 emit sigConnect(szFile,
true);
295void CFavoriteView::slotDelete()
297 auto lstIndex = selectionModel()->selectedIndexes();
298 foreach(
auto index, lstIndex)
299 m_pModel->removeRow(index.row(), index.parent());
302void CFavoriteView::slotNewGroup()
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())
309 QMessageBox::critical(
this, tr(
"Error"), tr(
"The group [%1] is existed").arg(szGroup));
313 QStandardItem* pGroup =
new QStandardItem(szGroup);
314 pGroup->setIcon(QIcon::fromTheme(
"network-workgroup"));
315 m_pModel->appendRow(pGroup);
318void CFavoriteView::dragEnterEvent(QDragEnterEvent *event)
320 qDebug(log) <<
"dragEnterEvent";
321 const CFavoriteMimeData* pData = qobject_cast<const CFavoriteMimeData*>(event->mimeData());
324 qDebug(log) <<
"dragEnterEvent acceptProposedAction";
325 event->acceptProposedAction();
329void CFavoriteView::dragMoveEvent(QDragMoveEvent *event)
333void CFavoriteView::dropEvent(QDropEvent *event)
335 qDebug(log) <<
"dropEvent";
336 const CFavoriteMimeData *pData = qobject_cast<const CFavoriteMimeData*>(event->mimeData());
338 QStandardItemModel* pModel =
dynamic_cast<QStandardItemModel*
>(model());
340#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
341 auto index = indexAt(event->position().toPoint());
343 auto index = indexAt(event->pos());
347 auto item = pModel->itemFromIndex(index);
348 if(item->data().isNull())
350 foreach(
auto i, pData->m_Items)
352 qDebug(log) <<
"dropEvent:" << item->text();
354 auto newItem = NewItem(i);
355 item->appendRow(newItem);
356 if(event->dropAction() == Qt::MoveAction)
357 pModel->removeRow(i.row(), i.parent());
360 qWarning(log) <<
"Don't group node. the data:" << item->data();
362 foreach(
auto i, pData->m_Items)
364 pModel->appendRow(NewItem(i));
365 if(event->dropAction() == Qt::MoveAction)
366 pModel->removeRow(i.row(), i.parent());
373QStandardItem* CFavoriteView::NewItem(
const QModelIndex &index)
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());
384void CFavoriteView::mousePressEvent(QMouseEvent *event)
386 if (event->button() == Qt::LeftButton)
387 m_DragStartPosition =
event->pos();
388 QTreeView::mousePressEvent(event);
391void CFavoriteView::mouseMoveEvent(QMouseEvent *event)
393 qDebug(log) <<
"mouseMoveEvent";
395 if (!(event->buttons() & Qt::LeftButton))
397 if ((event->pos() - m_DragStartPosition).manhattanLength()
398 < QApplication::startDragDistance())
400 qDebug(log) <<
"mouseMoveEvent drag";
401 QDrag *drag =
new QDrag(
this);
403 pData->m_Items = this->selectionModel()->selectedIndexes();
404 drag->setMimeData(pData);
406 Qt::DropAction dropAction = Qt::MoveAction;
407 if(event->modifiers() & Qt::ControlModifier)
408 dropAction = Qt::CopyAction;
409 drag->exec(dropAction);
412 QTreeView::mouseMoveEvent(event);