From 4b4ed69ab60001cef3acb90640bdb144a29206d3 Mon Sep 17 00:00:00 2001 From: Sergey Svistunov Date: Mon, 28 Oct 2024 13:45:10 +0300 Subject: [PATCH] qimageuploader: refactor UploadTreeView keyboard shortcuts --- Source/qimageuploader/CMakeLists.txt | 6 ++ Source/qimageuploader/Gui/MainWindow.cpp | 84 ++++++++++++++----- Source/qimageuploader/Gui/MainWindow.h | 5 ++ .../Gui/controls/UploadTreeView.cpp | 12 +-- 4 files changed, 81 insertions(+), 26 deletions(-) diff --git a/Source/qimageuploader/CMakeLists.txt b/Source/qimageuploader/CMakeLists.txt index 31a2ee6c..eba8a7c9 100644 --- a/Source/qimageuploader/CMakeLists.txt +++ b/Source/qimageuploader/CMakeLists.txt @@ -73,11 +73,17 @@ target_link_libraries(${PROJECT_NAME} Qt5::Widgets) if(WIN32) target_link_libraries(${PROJECT_NAME} gdi32 dwmapi) + if (IU_ENABLE_WEBVIEW2) + set_target_properties(${PROJECT_NAME} PROPERTIES VS_GLOBAL_WebView2LoaderPreference "Static") + set_target_properties(${PROJECT_NAME} PROPERTIES VS_USER_PROPS "${CMAKE_SOURCE_DIR}/ImageUploader.Core.props") + set_target_properties(${PROJECT_NAME} PROPERTIES VS_PACKAGE_REFERENCES "${WEBVIEW2_PACKAGES_REFERENCES}") + endif() endif() target_link_libraries(qimageuploader iucore iuvideo ${COMMON_LIBS_LIST}) if(WIN32) + add_custom_command(TARGET qimageuploader POST_BUILD COMMAND python "${CMAKE_SOURCE_DIR}/../Utils/Version/set_binary_version.py" "${CMAKE_SOURCE_DIR}/versioninfo.h" $ $ diff --git a/Source/qimageuploader/Gui/MainWindow.cpp b/Source/qimageuploader/Gui/MainWindow.cpp index 0c28dbd2..029caabe 100644 --- a/Source/qimageuploader/Gui/MainWindow.cpp +++ b/Source/qimageuploader/Gui/MainWindow.cpp @@ -66,9 +66,21 @@ MainWindow::MainWindow(CUploadEngineList* engineList, LogWindow* logWindow, QWid ui->treeView->setColumnWidth(1, 150); // Status column ui->treeView->setColumnWidth(2, 150); // Progress column ui->treeView->setSelectionBehavior(QAbstractItemView::SelectRows); + connect(ui->treeView, &QTreeView::doubleClicked, this, &MainWindow::itemDoubleClicked); connect(ui->treeView, &QTreeView::customContextMenuRequested, this, &MainWindow::onCustomContextMenu); + copyDirectLinkAction_ = new QAction(tr("Copy direct link"), this); + copyDirectLinkAction_->setShortcut(QKeySequence::Copy); + connect(copyDirectLinkAction_, &QAction::triggered, this, &MainWindow::onCopyDirectLinkTriggered); + + copyFilePathAction_ = new QAction(tr("Copy file path to clipboard"), this); + copyFilePathAction_->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_C)); + connect(copyFilePathAction_, &QAction::triggered, this, &MainWindow::onCopyFilePathTriggered); + + ui->treeView->addAction(copyDirectLinkAction_); + ui->treeView->addAction(copyFilePathAction_); + const ServerProfile imageProfile = settings->imageServer.getByIndex(0); imageServerWidget_ = new ServerSelectorWidget(uploadEngineManager_.get(), false, this); @@ -98,12 +110,12 @@ MainWindow::MainWindow(CUploadEngineList* engineList, LogWindow* logWindow, QWid iconsLoadingThread_->start(); - QMenu* contextMenu = new QMenu(this); - QAction* exitAction = contextMenu->addAction(tr("Exit")); + QMenu* trayContextMenu = new QMenu(this); + QAction* exitAction = trayContextMenu->addAction(tr("Exit")); connect(exitAction, &QAction::triggered, this, &MainWindow::quitApp); systemTrayIcon_ = new QSystemTrayIcon(this); systemTrayIcon_->setIcon(QIcon(":/res/icon_main.ico")); - systemTrayIcon_->setContextMenu(contextMenu); + systemTrayIcon_->setContextMenu(trayContextMenu); connect(systemTrayIcon_, &QSystemTrayIcon::activated, [&](QSystemTrayIcon::ActivationReason reason) { if (reason == QSystemTrayIcon::Trigger || reason == QSystemTrayIcon::DoubleClick){ setWindowState(windowState() & ~Qt::WindowMinimized | Qt::WindowActive); @@ -299,7 +311,7 @@ void MainWindow::onCustomContextMenu(const QPoint& point) { if (index.isValid()) { UploadTreeModel::InternalItem* internalItem = uploadTreeModel_->getInternalItem(index); QMenu* contextMenu = new QMenu(ui->treeView); - //ui->treeView->setContextMenuPolicy(Qt::ActionsContextMenu); + QAction* viewCodeAction = new QAction(tr("View HTML/BBCode"), contextMenu); contextMenu->addAction(viewCodeAction); @@ -315,19 +327,13 @@ void MainWindow::onCustomContextMenu(const QPoint& point) { QString viewUrl = QString::fromStdString(uploadResult->getDownloadUrl()); if (!directUrl.isEmpty()) { - QAction* copyDirectLinkAction = new QAction(tr("Copy direct link"), contextMenu); - copyDirectLinkAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_C)); - connect(copyDirectLinkAction, &QAction::triggered, [directUrl](bool checked) { - QClipboard* clipboard = QApplication::clipboard(); - clipboard->setText(directUrl); - }); - contextMenu->addAction(copyDirectLinkAction); + contextMenu->addAction(copyDirectLinkAction_); } if (!viewUrl.isEmpty()) { QAction* copyViewLinkAction = new QAction(tr("Copy view link"), contextMenu); if (directUrl.isEmpty()) { - copyViewLinkAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_C)); + copyViewLinkAction->setShortcut(QKeySequence::Copy); } connect(copyViewLinkAction, &QAction::triggered, [viewUrl](bool checked) { QClipboard* clipboard = QApplication::clipboard(); @@ -355,14 +361,7 @@ void MainWindow::onCustomContextMenu(const QPoint& point) { QDesktopServices::openUrl("file:///" + fileName); }); contextMenu->addAction(openInProgram); - - QAction* copyPathAction = new QAction(tr("Copy file path to clipboard"), contextMenu); - connect(copyPathAction, &QAction::triggered, [fileName](bool checked) - { - QClipboard* clipboard = QApplication::clipboard(); - clipboard->setText(fileName); - }); - contextMenu->addAction(copyPathAction); + contextMenu->addAction(copyFilePathAction_); } } @@ -381,6 +380,39 @@ void MainWindow::fillServerIcons() { fileServerWidget_->fillServerIcons(); } +void MainWindow::onCopyDirectLinkTriggered(bool checked) { + QModelIndex index = getUploadTreeViewSelectedIndex(); + + if (!index.isValid()) { + return; + } + + QString url = ui->treeView->model()->data(index, Qt::UserRole).toString(); + QClipboard* clipboard = QApplication::clipboard(); + clipboard->setText(url); +} + +void MainWindow::onCopyFilePathTriggered(bool checked) { + QModelIndex index = getUploadTreeViewSelectedIndex(); + + if (!index.isValid()) { + return; + } + + UploadTreeModel::InternalItem* internalItem = uploadTreeModel_->getInternalItem(index); + + if (!internalItem || !internalItem->task) { + return; + + } + auto fileTask = std::dynamic_pointer_cast(internalItem->task); + + if (fileTask) { + QString fileName = U2Q(fileTask->getFileName()); + QApplication::clipboard()->setText(fileName); + } +} + void MainWindow::on_actionAboutProgram_triggered() { AboutDialog dlg(this); dlg.setModal(true); @@ -415,3 +447,15 @@ void MainWindow::closeEvent(QCloseEvent *event) { saveOptions(); } +QModelIndex MainWindow::getUploadTreeViewSelectedIndex() { + QItemSelectionModel * selection = ui->treeView->selectionModel(); + QModelIndexList indexes = selection->selectedIndexes(); + + if(indexes.size() < 1) { + return {}; + } + std::sort(indexes.begin(), indexes.end()); + QModelIndex index = indexes.first(); + return index; +} + diff --git a/Source/qimageuploader/Gui/MainWindow.h b/Source/qimageuploader/Gui/MainWindow.h index 3f372728..05fd6750 100644 --- a/Source/qimageuploader/Gui/MainWindow.h +++ b/Source/qimageuploader/Gui/MainWindow.h @@ -47,6 +47,8 @@ private slots: void onCustomContextMenu(const QPoint &point); void onShowLog(); void fillServerIcons(); + void onCopyDirectLinkTriggered(bool checked); + void onCopyFilePathTriggered(bool checked); protected: bool addFileToList(QString fileName); bool addMultipleFilesToList(QStringList fileNames); @@ -55,6 +57,7 @@ private slots: void quitApp(); void saveOptions(); void closeEvent(QCloseEvent *event) override; + QModelIndex getUploadTreeViewSelectedIndex(); private: std::unique_ptr ui; UploadTreeModel* uploadTreeModel_; @@ -67,6 +70,8 @@ private slots: LogWindow* logWindow_; CUploadEngineList* engineList_; QThread* iconsLoadingThread_{}; + QAction* copyDirectLinkAction_ = nullptr; + QAction* copyFilePathAction_ = nullptr; }; #endif // MAINWINDOW_H diff --git a/Source/qimageuploader/Gui/controls/UploadTreeView.cpp b/Source/qimageuploader/Gui/controls/UploadTreeView.cpp index b66ad7b9..fd1eb632 100644 --- a/Source/qimageuploader/Gui/controls/UploadTreeView.cpp +++ b/Source/qimageuploader/Gui/controls/UploadTreeView.cpp @@ -11,15 +11,15 @@ UploadTreeView::UploadTreeView(QWidget *parent): QTreeView(parent) { void UploadTreeView::keyPressEvent(QKeyEvent * event) { - if(event->matches(QKeySequence::Copy)) { - copy(); - } else { + //if(event->matches(QKeySequence::Copy)) { + // copy(); + //} else { QTreeView::keyPressEvent(event); - } + ///} } void UploadTreeView::copy() { - QItemSelectionModel * selection = selectionModel(); + /*QItemSelectionModel * selection = selectionModel(); QModelIndexList indexes = selection->selectedIndexes(); if(indexes.size() < 1) { @@ -29,5 +29,5 @@ void UploadTreeView::copy() { QModelIndex index = indexes.first(); QString url = model()->data(index, Qt::UserRole).toString(); - QApplication::clipboard()->setText(url); + QApplication::clipboard()->setText(url);*/ }