Skip to content

Commit d698c0b

Browse files
committed
App: drag json to import recent
1 parent 399639a commit d698c0b

3 files changed

Lines changed: 116 additions & 7 deletions

File tree

App/Client/Recent/FrmRecent.cpp

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Author: Kang Lin <kl222@126.com>
22

3+
#include <QMimeData>
34
#include <QVBoxLayout>
45
#include <QDateTime>
56
#include <QStandardItem>
@@ -13,7 +14,7 @@
1314
#include "FrmRecent.h"
1415
#include "RabbitCommonDir.h"
1516

16-
static Q_LOGGING_CATEGORY(log, "App.FrmListRecent")
17+
static Q_LOGGING_CATEGORY(log, "App.CFrmRecent")
1718

1819
CFrmRecent::CFrmRecent(
1920
MainWindow *pMainWindow, CManager *pManager,
@@ -156,6 +157,7 @@ CFrmRecent::CFrmRecent(
156157

157158
Q_ASSERT(m_pManager);
158159
m_pTableView = new QTableView(this);
160+
InitialDrop();
159161
m_pTableView->setContextMenuPolicy(Qt::CustomContextMenu);
160162
check = connect(m_pTableView,
161163
SIGNAL(customContextMenuRequested(const QPoint &)),
@@ -582,9 +584,10 @@ void CFrmRecent::slotImport()
582584
this, tr("Success"),
583585
tr("Successfully imported recent from json file"));
584586
} else {
585-
QMessageBox::warning(
586-
this, tr("Failure"),
587-
tr("Failed to import recent from json file"));
587+
QString szErr = tr("Failed to import recent from json file: %1").arg(filename) + "\n";
588+
if(!m_Database.GetError().isEmpty())
589+
szErr += "\n" + tr("Error: ") + m_Database.GetError();
590+
QMessageBox::critical(this, tr("Failure"), szErr);
588591
}
589592
return;
590593
}
@@ -606,11 +609,89 @@ void CFrmRecent::slotExport()
606609
this, tr("Success"),
607610
tr("Successfully exported recent to json file"));
608611
} else {
609-
QMessageBox::warning(
610-
this, tr("Failure"),
611-
tr("Failed to export recent to json file"));
612+
QString szErr = tr("Failed to export recent to json file: %1").arg(filename) + "\n";
613+
if(!m_Database.GetError().isEmpty())
614+
szErr += "\n" + tr("Error: ") + m_Database.GetError();
615+
QMessageBox::critical(this, tr("Failure"), szErr);
612616
}
613617
return;
614618
}
615619
}
616620
}
621+
622+
void CFrmRecent::InitialDrop()
623+
{
624+
if(!m_pTableView || !m_pTableView->viewport())
625+
return;
626+
m_pTableView->viewport()->installEventFilter(this);
627+
m_pTableView->setAcceptDrops(true);
628+
//m_pTableView->setDragEnabled(true);
629+
m_pTableView->setDragDropMode(QTreeView::DropOnly);
630+
m_pTableView->setDefaultDropAction(Qt::MoveAction);
631+
m_pTableView->setDropIndicatorShown(true);
632+
}
633+
634+
void CFrmRecent::dragEnterEvent(QDragEnterEvent *event)
635+
{
636+
//qDebug(log) << Q_FUNC_INFO;
637+
auto urls = event->mimeData()->urls();
638+
if(event->mimeData()->hasUrls() && urls.length() == 1) {
639+
//qDebug(log) << event->mimeData()->urls();
640+
event->acceptProposedAction();
641+
return;
642+
}
643+
}
644+
645+
void CFrmRecent::dragMoveEvent(QDragMoveEvent *event)
646+
{
647+
//qDebug(log) << Q_FUNC_INFO;
648+
}
649+
650+
void CFrmRecent::dropEvent(QDropEvent *event)
651+
{
652+
//qDebug(log) << Q_FUNC_INFO;
653+
bool bRet = false;
654+
auto urls = event->mimeData()->urls();
655+
foreach(auto url, urls)
656+
{
657+
if(url.isLocalFile()) {
658+
QString filename = url.toLocalFile();
659+
if (m_Database.ImportFromJsonFile(filename)) {
660+
slotRefresh();
661+
emit sigShowMessageBox(tr("Success"),
662+
tr("Successfully imported recent from json file"),
663+
QMessageBox::Information);
664+
} else {
665+
QString szErr = tr("Failed to import recent from json file: %1").arg(filename) + "\n";
666+
if(!m_Database.GetError().isEmpty())
667+
szErr += "\n" + tr("Error: ") + m_Database.GetError();
668+
emit sigShowMessageBox(tr("Failure"), szErr, QMessageBox::Critical);
669+
}
670+
}
671+
}
672+
if(bRet)
673+
event->accept();
674+
else
675+
event->ignore();
676+
}
677+
678+
bool CFrmRecent::eventFilter(QObject *watched, QEvent *event)
679+
{
680+
if (watched == m_pTableView->viewport()) {
681+
//qDebug(log) << Q_FUNC_INFO << "Viewport event:" << event->type();
682+
switch (event->type()) {
683+
case QEvent::DragEnter:
684+
dragEnterEvent(static_cast<QDragEnterEvent*>(event));
685+
return true;
686+
case QEvent::DragMove:
687+
dragMoveEvent(static_cast<QDragMoveEvent*>(event));
688+
return true;
689+
case QEvent::Drop:
690+
dropEvent(static_cast<QDropEvent*>(event));
691+
return true;
692+
default:
693+
break;
694+
}
695+
}
696+
return QWidget::eventFilter(watched, event);
697+
}

App/Client/Recent/FrmRecent.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ public Q_SLOTS:
4848
const QString& szName,
4949
const QString& szDescription,
5050
const QIcon& icon);
51+
/*!
52+
* \~chinese
53+
* \brief 用消息对话框(QMessageBox)显示信息
54+
*
55+
* \~english
56+
* \brief Use message box display information
57+
*
58+
* \~
59+
* \see COperate::sigShowMessageBox()
60+
*/
61+
virtual void sigShowMessageBox(const QString& title, const QString& message,
62+
const QMessageBox::Icon& icon);
5163

5264
private slots:
5365
void slotEditConnect();
@@ -91,4 +103,14 @@ private slots:
91103
CRecentModel* m_pModel;
92104
CManager* m_pManager;
93105
bool m_bDock;
106+
107+
private:
108+
void InitialDrop();
109+
virtual void dragEnterEvent(QDragEnterEvent *event) override;
110+
virtual void dragMoveEvent(QDragMoveEvent *event) override;
111+
virtual void dropEvent(QDropEvent *event) override;
112+
113+
// QObject interface
114+
public:
115+
virtual bool eventFilter(QObject *watched, QEvent *event) override;
94116
};

App/Client/mainwindow.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ MainWindow::MainWindow(QWidget *parent)
231231
check = connect(m_pFavoriteView, SIGNAL(sigFavorite()),
232232
this, SLOT(on_actionAdd_to_favorite_triggered()));
233233
Q_ASSERT(check);
234+
check = connect(m_pFavoriteView, &CFavoriteView::sigShowMessageBox,
235+
this, &MainWindow::slotShowMessageBox);
236+
Q_ASSERT(check);
234237
check = connect(&m_Parameter, SIGNAL(sigFavoriteEditChanged(bool)),
235238
m_pFavoriteView, SLOT(slotDoubleEditNode(bool)));
236239
Q_ASSERT(check);
@@ -265,6 +268,9 @@ MainWindow::MainWindow(QWidget *parent)
265268
check = connect(m_pRecent, &CFrmRecent::sigAddToFavorite,
266269
m_pFavoriteView, &CFavoriteView::slotAddToFavorite);
267270
Q_ASSERT(check);
271+
check = connect(m_pRecent, &CFrmRecent::sigShowMessageBox,
272+
this, &MainWindow::slotShowMessageBox);
273+
Q_ASSERT(check);
268274
m_pDockRecent->setWidget(m_pRecent);
269275
m_pDockRecent->setWindowTitle(
270276
m_pRecent->windowTitle());

0 commit comments

Comments
 (0)