Skip to content

Commit a760b03

Browse files
committed
App: drag json to import favorte
1 parent fcbe36c commit a760b03

5 files changed

Lines changed: 91 additions & 52 deletions

File tree

App/Client/Favorite/FavoriteDatabase.cpp

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,20 @@ bool CFavoriteDatabase::OnInitializeSqliteDatabase()
2323
QSqlQuery query(GetDatabase());
2424

2525
// Create favorite table
26-
bool success = query.exec(
26+
QString szSql =
2727
"CREATE TABLE IF NOT EXISTS favorite ("
2828
" id INTEGER PRIMARY KEY AUTOINCREMENT," // the id is the key of tree table
2929
" name TEXT NOT NULL,"
3030
" icon INTEGER DEFAULT 0,"
3131
" file TEXT UNIQUE NOT NULL,"
3232
" description TEXT"
33-
")"
34-
);
33+
")";
34+
bool success = query.exec(szSql);
3535

3636
if (!success) {
37-
qCritical(log) << "Failed to create favorite table:" << query.lastError().text();
37+
SetError("Failed to create favorite table: " + query.lastError().text()
38+
+ "; Sql: " + szSql);
39+
qCritical(log) << GetError();
3840
return false;
3941
}
4042

@@ -49,7 +51,7 @@ bool CFavoriteDatabase::OnInitializeSqliteDatabase()
4951
qDebug(log) << "Failed to drop trigger delete_icon_after_favorite:" << query.lastError().text();
5052
}
5153
// Create trigger
52-
QString szSql = R"(
54+
szSql = R"(
5355
CREATE TRIGGER delete_icon_after_favorite
5456
AFTER DELETE ON favorite
5557
FOR EACH ROW
@@ -79,20 +81,21 @@ bool CFavoriteDatabase::OnInitializeMySqlDatabase()
7981
QSqlQuery query(GetDatabase());
8082

8183
// Create favorite table
82-
success = query.exec(
84+
QString szSql =
8385
"CREATE TABLE IF NOT EXISTS favorite ("
8486
" id INTEGER PRIMARY KEY AUTO_INCREMENT," // the id is the key of tree table
8587
" name TEXT NOT NULL,"
8688
" icon INTEGER DEFAULT 0,"
8789
" file TEXT NOT NULL,"
8890
" description TEXT,"
8991
" UNIQUE KEY uk_favorite_file (file(255))"
90-
")"
91-
);
92+
")";
93+
success = query.exec(szSql);
9294
if (!success) {
93-
qCritical(log) << "Failed to create favorite table:"
94-
<< query.lastError().text()
95-
<< "Sql:" << query.executedQuery();
95+
SetError("Failed to create favorite table: "
96+
+ query.lastError().text()
97+
+ "; Sql: " + szSql);
98+
qCritical(log) << GetError();
9699
return false;
97100
}
98101

@@ -103,7 +106,7 @@ bool CFavoriteDatabase::OnInitializeMySqlDatabase()
103106
<< "Sql:" << query.executedQuery();
104107
}
105108
// Create trigger
106-
QString szSql = R"(
109+
szSql = R"(
107110
CREATE TRIGGER delete_icon_after_favorite
108111
AFTER DELETE ON favorite
109112
FOR EACH ROW
@@ -177,7 +180,8 @@ int CFavoriteDatabase::AddFavorite(const QString &szFile,
177180
bool success = false;
178181
// 使用事务确保数据一致性
179182
if (!db.transaction()) {
180-
qCritical(log) << "Failed to start transaction:" << db.lastError().text();
183+
SetError("Failed to start transaction: " + db.lastError().text());
184+
qCritical(log) << GetError();
181185
return ret;
182186
}
183187

@@ -208,15 +212,13 @@ int CFavoriteDatabase::AddFavorite(const QString &szFile,
208212
if (!success) {
209213
QString szErr = "Failed to insert favorite table: " + query.lastError().text();
210214
SetError(szErr);
211-
qCritical(log) << szErr;
212215
throw std::runtime_error(szErr.toStdString());
213216
}
214217

215218
key = query.lastInsertId().toInt();
216219
if(0 >= key) {
217220
QString szErr = "Failed to insert favorite table";
218221
SetError(szErr);
219-
qCritical(log) << szErr;
220222
throw std::runtime_error(szErr.toStdString());
221223
}
222224
}
@@ -229,15 +231,13 @@ int CFavoriteDatabase::AddFavorite(const QString &szFile,
229231
if (0 >= id) {
230232
QString szErr = "Failed to insert favorite folder table";
231233
SetError(szErr);
232-
qCritical(log) << szErr;
233234
throw std::runtime_error(szErr.toStdString());
234235
}
235236

236237
// 提交事务
237238
if (!db.commit()) {
238239
QString szErr = "Failed to commit transaction:" + db.lastError().text();
239240
SetError(szErr);
240-
qCritical(log) << szErr;
241241
throw std::runtime_error(szErr.toStdString());
242242
}
243243

@@ -277,10 +277,11 @@ bool CFavoriteDatabase::UpdateFavorite(
277277
szSql = "UPDATE favorite SET " + szSql + " WHERE id=" + QString::number(id);
278278
//qDebug(log) << "Sql:" << szSql;
279279
bool ok = query.exec(szSql);
280-
if(!ok)
281-
qCritical(log) << "Failed to update favorite:"
282-
<< id << query.lastError().text()
283-
<< "Sql:" << szSql;
280+
if(!ok) {
281+
SetError("Failed to update favorite: " + query.lastError().text()
282+
+ "; Sql: " + szSql);
283+
qCritical(log) << GetError();
284+
}
284285
return ok;
285286
}
286287

@@ -306,10 +307,11 @@ bool CFavoriteDatabase::UpdateFavorite(
306307
szSql = "UPDATE favorite SET " + szSql + " WHERE file=\"" + szFile + "\"";
307308
//qDebug(log) << "Sql:" << szSql;
308309
bool ok = query.exec(szSql);
309-
if(!ok)
310-
qCritical(log) << "Failed to update favorite:"
311-
<< szFile << query.lastError().text()
312-
<< "Sql:" << szSql;
310+
if(!ok) {
311+
SetError("Failed to update favorite: " + query.lastError().text()
312+
+ "; Sql: " + szSql);
313+
qCritical(log) << GetError();
314+
}
313315
return ok;
314316
}
315317

@@ -330,9 +332,10 @@ CFavoriteDatabase::Item CFavoriteDatabase::GetFavorite(int id)
330332
//qDebug(log) << "Bound value:" << query.boundValues();
331333
bool ok = query.exec();
332334
if(!ok) {
333-
qCritical(log) << "Failed to get favorite:"
334-
<< id << query.lastError().text()
335-
<< "Sql:" << query.executedQuery();
335+
SetError("Failed to get favorite: " + query.lastError().text()
336+
+ "; Sql: " + query.executedQuery()
337+
+ "; id: " + QString::number(id));
338+
qCritical(log) << GetError();
336339
return item;
337340
}
338341
if(query.next()) {
@@ -362,9 +365,9 @@ QList<CFavoriteDatabase::Item> CFavoriteDatabase::GetFavorite(const QString &szF
362365
//qDebug(log) << "Bound value:" << query.boundValues();
363366
bool ok = query.exec();
364367
if(!ok) {
365-
qCritical(log) << "Failed to get favorite:"
366-
<< szFile << query.lastError().text()
367-
<< "Sql:" << query.executedQuery();
368+
SetError("Failed to get favorite: " + query.lastError().text()
369+
+ "; Sql: " + query.executedQuery() + "; szFile: " + szFile);;
370+
qCritical(log) << GetError();
368371
return lstItems;
369372
}
370373
if(query.next()) {
@@ -427,9 +430,10 @@ bool CFavoriteDatabase::OnDeleteKey(int key)
427430
//qDebug(log) << "Bound value:" << query.boundValues();
428431
bool ok = query.exec();
429432
if(!ok) {
430-
qCritical(log) << "Failed to delete favorite:"
431-
<< key << query.lastError().text()
432-
<< "Sql:" << query.executedQuery();
433+
SetError("Failed to delete favorite: " + query.lastError().text()
434+
+ "; Sql: " + query.executedQuery()
435+
+ "; key: " + QString::number(key));
436+
qCritical(log) << GetError();
433437
return false;
434438
}
435439
return ok;
@@ -449,7 +453,8 @@ bool CFavoriteDatabase::ImportFromJson(const QJsonObject &obj)
449453
{
450454
QJsonArray favorites = obj["favorite"].toArray();
451455
if(favorites.isEmpty()) {
452-
qCritical(log) << "The file format is error. Json without favorite";
456+
SetError(tr("The file format is error. Json without favorite"));
457+
qCritical(log) << GetError();
453458
return false;
454459
}
455460

App/Client/Favorite/FavoriteView.cpp

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,8 @@ void CFavoriteView::slotImport()
476476
} else {
477477
QMessageBox::critical(
478478
this, tr("Import favorite"),
479-
tr("Failed to import favorite from JSON file: %1").arg(filename));
479+
tr("Failed to import favorite from JSON file: %1").arg(filename) + "\n"
480+
+ tr("Error: ") + m_pDatabase->GetError());
480481
}
481482
return;
482483
}
@@ -503,7 +504,8 @@ void CFavoriteView::slotExport()
503504
} else {
504505
QMessageBox::critical(
505506
this, tr("Export favorite"),
506-
tr("Failed to export favorite to JSON file: %1").arg(filename));
507+
tr("Failed to export favorite to JSON file: %1").arg(filename) + "\n"
508+
+ tr("Error: ") + m_pDatabase->GetError());
507509
}
508510
return;
509511
}
@@ -516,6 +518,12 @@ void CFavoriteView::slotExport()
516518
void CFavoriteView::dragEnterEvent(QDragEnterEvent *event)
517519
{
518520
qDebug(log) << Q_FUNC_INFO;
521+
if(event->mimeData()->hasUrls()) {
522+
//qDebug(log) << event->mimeData()->urls();
523+
event->acceptProposedAction();
524+
return;
525+
}
526+
519527
const CFavoriteMimeData* pData =
520528
qobject_cast<const CFavoriteMimeData*>(event->mimeData());
521529
if (pData)
@@ -535,27 +543,47 @@ void CFavoriteView::dragEnterEvent(QDragEnterEvent *event)
535543

536544
void CFavoriteView::dragMoveEvent(QDragMoveEvent *event)
537545
{
538-
qDebug(log) << Q_FUNC_INFO;
539-
const CFavoriteMimeData* pData =
540-
qobject_cast<const CFavoriteMimeData*>(event->mimeData());
541-
if (!pData)
542-
{
543-
event->ignore();
544-
return;
545-
}
546+
// qDebug(log) << Q_FUNC_INFO;
547+
// const CFavoriteMimeData* pData =
548+
// qobject_cast<const CFavoriteMimeData*>(event->mimeData());
549+
// if (!pData)
550+
// {
551+
// event->ignore();
552+
// return;
553+
// }
546554
}
547555

548556
void CFavoriteView::dropEvent(QDropEvent *event)
549557
{
550558
qDebug(log) << Q_FUNC_INFO << "drop action:" << event->dropAction();
559+
bool bRet = false;
560+
auto urls = event->mimeData()->urls();
561+
foreach(auto url, urls)
562+
{
563+
if(url.isLocalFile()) {
564+
QString filename = url.toLocalFile();
565+
if (m_pDatabase->ImportFromJsonFile(filename)) {
566+
slotRefresh();
567+
QMessageBox::information(
568+
this, tr("Import favorite"),
569+
tr("Successfully imported favorite from JSON file: %1").arg(filename));
570+
} else {
571+
QMessageBox::critical(
572+
this, tr("Import favorite"),
573+
tr("Failed to import favorite from JSON file: %1").arg(filename) + "\n"
574+
+ tr("Error: ") + m_pDatabase->GetError());
575+
}
576+
}
577+
}
578+
if(bRet)
579+
event->accept();
551580

552581
const CFavoriteMimeData *pData = qobject_cast<const CFavoriteMimeData*>(event->mimeData());
553582
if(!pData || pData->m_Items.isEmpty() || !m_pTreeView || !m_pModel) {
554583
event->ignore();
555584
return;
556585
}
557586

558-
bool bRet = false;
559587
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
560588
auto idxParent = m_pTreeView->indexAt(event->position().toPoint());
561589
#else
@@ -654,7 +682,7 @@ bool CFavoriteView::eventFilter(QObject *watched, QEvent *event)
654682
{
655683
// 处理 treeview 的事件
656684
if (watched == m_pTreeView->viewport()) { // 处理 viewport 的事件
657-
qDebug(log) << Q_FUNC_INFO << "Viewport event:" << event->type();
685+
//qDebug(log) << Q_FUNC_INFO << "Viewport event:" << event->type();
658686

659687
switch (event->type()) {
660688
case QEvent::DragEnter:

App/Client/android/AndroidManifest.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@
4242
android:label="@string/app_name"
4343
android:launchMode="singleTop"
4444
android:screenOrientation="unspecified"
45-
android:exported="true"
46-
android:windowSoftInputMode="adjustResize|stateHidden">
47-
45+
android:exported="true">
46+
<!-- android:windowSoftInputMode="adjustResize|stateHidden" -->
47+
4848
<intent-filter>
4949
<action android:name="android.intent.action.MAIN"/>
5050
<category android:name="android.intent.category.LAUNCHER"/>

App/Client/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void InitAndroidVirtualKeyboard()
6767
//qputenv("QT_VIRTUALKEYBOARD_HANDWRITING", QByteArray("1"));
6868

6969
// 启用调试
70-
qputenv("QT_DEBUG_IM", "1");
70+
//qputenv("QT_DEBUG_IM", "1");
7171
}
7272

7373
int main(int argc, char *argv[])

App/Client/mainwindow.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1651,7 +1651,7 @@ void MainWindow::dragEnterEvent(QDragEnterEvent *event)
16511651

16521652
if(event->mimeData()->hasUrls())
16531653
{
1654-
qWarning(log) << event->mimeData()->urls();
1654+
//qDebug(log) << event->mimeData()->urls();
16551655
event->acceptProposedAction();
16561656
}
16571657
}
@@ -1664,14 +1664,20 @@ void MainWindow::dragMoveEvent(QDragMoveEvent *event)
16641664
void MainWindow::dropEvent(QDropEvent *event)
16651665
{
16661666
qDebug(log) << "dropEvent";
1667+
bool bRet = false;
16671668
if(!event->mimeData()->hasUrls())
16681669
return;
16691670
auto urls = event->mimeData()->urls();
16701671
foreach(auto url, urls)
16711672
{
16721673
if(url.isLocalFile())
16731674
slotOpenFile(url.toLocalFile());
1675+
bRet = true;
16741676
}
1677+
if(bRet)
1678+
event->accept();
1679+
else
1680+
event->ignore();
16751681
}
16761682

16771683
void MainWindow::slotSystemTrayIconActivated(QSystemTrayIcon::ActivationReason reason)

0 commit comments

Comments
 (0)