From e6a0c27b74d1f330d9e18ba016178a47972a5e8b Mon Sep 17 00:00:00 2001 From: Liu Zhangjian Date: Thu, 14 Nov 2024 16:19:54 +0800 Subject: [PATCH] refactor: [recent] Refactor recent plugin structure - Removed redundant files and simplified CMakeLists.txt using GLOB_RECURSE - Renamed recentdisplay.h/cpp to recentdisplaywidget.h/cpp for better naming - Removed unused displayitemdelegate and displayrecentview files Log: Refactored recent plugin structure --- src/common/settings/settings.cpp | 2 +- src/plugins/recent/CMakeLists.txt | 26 +- .../recent/mainframe/displayrecentview.cpp | 168 ----- .../recent/mainframe/displayrecentview.h | 42 -- ...splayitemdelegate.cpp => itemdelegate.cpp} | 18 +- .../{displayitemdelegate.h => itemdelegate.h} | 10 +- src/plugins/recent/mainframe/itemlistview.cpp | 67 ++ src/plugins/recent/mainframe/itemlistview.h | 37 ++ .../recent/mainframe/recentdisplay.cpp | 576 ------------------ .../recent/mainframe/recentdisplaywidget.cpp | 416 +++++++++++++ ...{recentdisplay.h => recentdisplaywidget.h} | 27 +- .../recent/mainframe/recentlistview.cpp | 140 +++++ src/plugins/recent/mainframe/recentlistview.h | 51 ++ src/plugins/recent/recent.cpp | 8 +- 14 files changed, 751 insertions(+), 837 deletions(-) delete mode 100644 src/plugins/recent/mainframe/displayrecentview.cpp delete mode 100644 src/plugins/recent/mainframe/displayrecentview.h rename src/plugins/recent/mainframe/{displayitemdelegate.cpp => itemdelegate.cpp} (90%) rename src/plugins/recent/mainframe/{displayitemdelegate.h => itemdelegate.h} (87%) create mode 100644 src/plugins/recent/mainframe/itemlistview.cpp create mode 100644 src/plugins/recent/mainframe/itemlistview.h delete mode 100644 src/plugins/recent/mainframe/recentdisplay.cpp create mode 100644 src/plugins/recent/mainframe/recentdisplaywidget.cpp rename src/plugins/recent/mainframe/{recentdisplay.h => recentdisplaywidget.h} (62%) create mode 100644 src/plugins/recent/mainframe/recentlistview.cpp create mode 100644 src/plugins/recent/mainframe/recentlistview.h diff --git a/src/common/settings/settings.cpp b/src/common/settings/settings.cpp index 9992e36ec..f324ea134 100644 --- a/src/common/settings/settings.cpp +++ b/src/common/settings/settings.cpp @@ -210,7 +210,7 @@ void Settings::setValue(const QString &group, const QString &key, const QVariant if (d->isRemovable(group, key)) { changed = d->settingData.value(group, key) != value; } else { - changed = this->value(group, key, value) != value; + changed = this->value(group, key) != value; } if (!changed) diff --git a/src/plugins/recent/CMakeLists.txt b/src/plugins/recent/CMakeLists.txt index 8937c0415..99ee86dc3 100644 --- a/src/plugins/recent/CMakeLists.txt +++ b/src/plugins/recent/CMakeLists.txt @@ -2,27 +2,17 @@ cmake_minimum_required(VERSION 3.0.2) project(recent) -set(CXX_CPP - mainframe/displayrecentview.cpp - mainframe/displayitemdelegate.cpp - mainframe/recentdisplay.cpp - transceiver/recentreceiver.cpp - recent.cpp - recent.json - ) - -set(CXX_H - mainframe/displayrecentview.h - mainframe/displayitemdelegate.h - mainframe/recentdisplay.h - transceiver/recentreceiver.h - recent.h - ) +FILE(GLOB_RECURSE PROJECT_SOURCES + "${CMAKE_CURRENT_SOURCE_DIR}/*.h" + "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/*/*.h" + "${CMAKE_CURRENT_SOURCE_DIR}/*/*.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/*.json" +) add_library(${PROJECT_NAME} SHARED - ${CXX_CPP} - ${CXX_H} + ${PROJECT_SOURCES} recent.qrc ) diff --git a/src/plugins/recent/mainframe/displayrecentview.cpp b/src/plugins/recent/mainframe/displayrecentview.cpp deleted file mode 100644 index bef35ea9d..000000000 --- a/src/plugins/recent/mainframe/displayrecentview.cpp +++ /dev/null @@ -1,168 +0,0 @@ -// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. -// -// SPDX-License-Identifier: GPL-3.0-or-later - -#include "displayrecentview.h" -#include "common/common.h" -#include "displayitemdelegate.h" - -#include -#include - -#include - -#include -#include -#include -#include -#include - -#define INIT_DATA "{\n \"Projects\":[],\n \"Documents\":[]\n}\n" - -DisplayRecentView::DisplayRecentView(QWidget *parent) - : DListView (parent) - , cache({}) - , model(new QStandardItemModel(this)) -{ - setDragDropMode(QAbstractItemView::NoDragDrop); - setSelectionMode(QAbstractItemView::SingleSelection); - setSelectionBehavior(QAbstractItemView::SelectRows); - setEditTriggers(QListView::NoEditTriggers); - setTextElideMode(Qt::ElideMiddle); - setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); - setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - setAlternatingRowColors(false); - setSelectionRectVisible(true); - setMouseTracking(true); - - setUniformItemSizes(true); - setResizeMode(Fixed); - setOrientation(QListView::TopToBottom, false); - setFrameStyle(QFrame::NoFrame); - setSpacing(0); - setContentsMargins(0, 0, 0, 0); - - setModel(model); - setItemDelegate(new DisplayItemDelegate(this)); -} - -DisplayRecentView::~DisplayRecentView() -{ - -} - -QJsonDocument DisplayRecentView::readRecent() -{ - QJsonDocument recentDoc; - QFile file(cachePath()); - if (!file.exists()) { - if (file.open(QFile::Truncate| QFile::WriteOnly)) { - file.write(INIT_DATA); - file.close(); - } - } else { - if (file.open(QFile::ReadOnly)) { - recentDoc = QJsonDocument::fromJson(file.readAll()); - file.close(); - } - - if (!recentDoc.object().keys().contains("Projects") - || !recentDoc.object().keys().contains("Documents")) { - file.remove(); - file.close(); - file.open(QFile::Truncate| QFile::WriteOnly); - file.write(INIT_DATA); - file.close(); - } - } - - if (file.open(QFile::ReadOnly)) { - recentDoc = QJsonDocument::fromJson(file.readAll()); - file.close(); - } - return recentDoc; -} - -QList DisplayRecentView::itemsFromFile() -{ - QJsonDocument doc = readRecent(); - QJsonObject obj = doc.object(); - QJsonArray array = obj.value(title()).toArray(); - QList result; - for (auto one : array) { - QString filePath(one.toString()); - auto rowItem = new DStandardItem (icon(filePath), filePath); - rowItem->setToolTip(filePath); - if (!cache.contains(filePath)) { - cache << filePath; - } - result << rowItem; - } - return result; -} - -void DisplayRecentView::focusOutEvent(QFocusEvent *e) -{ - DListView::clearSelection(); - - DListView::focusOutEvent(e); -} - -void DisplayRecentView::mousePressEvent(QMouseEvent *e) -{ - if (e->button() != Qt::LeftButton) - return; - return DListView::mousePressEvent(e); -} - -QString DisplayRecentView::cachePath() -{ - return CustomPaths::user(CustomPaths::Configures) - + QDir::separator() + "recent.support"; -} - -void DisplayRecentView::add(const QString &data) -{ - model->clear(); //删除数据 - if (cache.contains(data)) { - cache.removeOne(data); - } - cache.insert(0, data); //置顶 - saveToFile(cache); //保存序列 - load(); //重新加载文件 -} - -QIcon DisplayRecentView::icon(const QString &data) -{ - QFileInfo info(data); - return iconProvider.icon(info); -} - -void DisplayRecentView::load() -{ - model->appendColumn(itemsFromFile()); -} - -void DisplayRecentView::clearData() -{ - cache.clear(); - model->clear(); -} - -void DisplayRecentView::saveToFile(const QStringList &cache) -{ - QJsonDocument doc = readRecent(); - QJsonObject docObj = doc.object(); - QJsonArray array; - for (auto item : cache) { - array << item; - } - docObj[title()] = array; - doc.setObject(docObj); - - QFile file(cachePath()); - if (file.open(QFile::WriteOnly)) { - file.write(doc.toJson()); - file.close(); - } -} diff --git a/src/plugins/recent/mainframe/displayrecentview.h b/src/plugins/recent/mainframe/displayrecentview.h deleted file mode 100644 index ac093feaf..000000000 --- a/src/plugins/recent/mainframe/displayrecentview.h +++ /dev/null @@ -1,42 +0,0 @@ -// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. -// -// SPDX-License-Identifier: GPL-3.0-or-later - -#ifndef DISPLAYRECENTVIEW_H -#define DISPLAYRECENTVIEW_H - -#include -#include - -DWIDGET_USE_NAMESPACE - -class QStandardItemModel; -class QStandardItem; -class DisplayRecentView : public DListView -{ - Q_OBJECT - QStringList cache; - DFileIconProvider iconProvider; -public: - explicit DisplayRecentView(QWidget *parent = nullptr); - ~DisplayRecentView() override; - - virtual QString cachePath(); - virtual void add(const QString &data); - virtual QIcon icon(const QString &data); - virtual QString title() = 0; - virtual void load(); - virtual void clearData(); - virtual QList itemsFromFile(); - - void focusOutEvent(QFocusEvent *e) override; - void mousePressEvent(QMouseEvent *e) override; -protected: - virtual void saveToFile(const QStringList &cache); -protected: - QStandardItemModel *model; - QJsonDocument readRecent(); -private: -}; - -#endif // DISPLAYRECENTVIEW_H diff --git a/src/plugins/recent/mainframe/displayitemdelegate.cpp b/src/plugins/recent/mainframe/itemdelegate.cpp similarity index 90% rename from src/plugins/recent/mainframe/displayitemdelegate.cpp rename to src/plugins/recent/mainframe/itemdelegate.cpp index 36d1e434a..142cc3e76 100644 --- a/src/plugins/recent/mainframe/displayitemdelegate.cpp +++ b/src/plugins/recent/mainframe/itemdelegate.cpp @@ -2,8 +2,8 @@ // // SPDX-License-Identifier: GPL-3.0-or-later -#include "displayitemdelegate.h" -#include "displayrecentview.h" +#include "itemdelegate.h" +#include "itemlistview.h" #include #include @@ -30,12 +30,12 @@ inline constexpr int kIconHeight = { 30 }; inline constexpr int kIconLeftMargin = { 10 }; inline constexpr int kTextLeftMargin = { 8 }; -DisplayItemDelegate::DisplayItemDelegate(QAbstractItemView *parent) +ItemDelegate::ItemDelegate(QAbstractItemView *parent) : DStyledItemDelegate(parent) { } -void DisplayItemDelegate::paint(QPainter *painter, +void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { @@ -57,7 +57,7 @@ void DisplayItemDelegate::paint(QPainter *painter, painter->setOpacity(1); } -QSize DisplayItemDelegate::sizeHint(const QStyleOptionViewItem &option, +QSize ItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { if (index.isValid()) { @@ -71,7 +71,7 @@ QSize DisplayItemDelegate::sizeHint(const QStyleOptionViewItem &option, return DStyledItemDelegate::sizeHint(option, index); } -void DisplayItemDelegate::paintItemBackground(QPainter *painter, const QStyleOptionViewItem &option, +void ItemDelegate::paintItemBackground(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { painter->save(); @@ -110,7 +110,7 @@ void DisplayItemDelegate::paintItemBackground(QPainter *painter, const QStyleOpt painter->restore(); } -QRectF DisplayItemDelegate::paintItemIcon(QPainter *painter, const QStyleOptionViewItem &option, +QRectF ItemDelegate::paintItemIcon(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { painter->save(); @@ -149,7 +149,7 @@ QRectF DisplayItemDelegate::paintItemIcon(QPainter *painter, const QStyleOptionV return iconRect; } -QPixmap DisplayItemDelegate::getIconPixmap(const QIcon &icon, const QSize &size, qreal pixelRatio = 1.0, QIcon::Mode mode, QIcon::State state) +QPixmap ItemDelegate::getIconPixmap(const QIcon &icon, const QSize &size, qreal pixelRatio = 1.0, QIcon::Mode mode, QIcon::State state) { if (icon.isNull()) return QPixmap(); @@ -164,7 +164,7 @@ QPixmap DisplayItemDelegate::getIconPixmap(const QIcon &icon, const QSize &size, return px; } -void DisplayItemDelegate::paintItemColumn(QPainter *painter, const QStyleOptionViewItem &option, +void ItemDelegate::paintItemColumn(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index, const QRectF &iconRect) const { painter->save(); diff --git a/src/plugins/recent/mainframe/displayitemdelegate.h b/src/plugins/recent/mainframe/itemdelegate.h similarity index 87% rename from src/plugins/recent/mainframe/displayitemdelegate.h rename to src/plugins/recent/mainframe/itemdelegate.h index 1bd9ae0af..7b950a665 100644 --- a/src/plugins/recent/mainframe/displayitemdelegate.h +++ b/src/plugins/recent/mainframe/itemdelegate.h @@ -2,14 +2,14 @@ // // SPDX-License-Identifier: GPL-3.0-or-later -#ifndef DISPLAYITEMDELEGATE_H -#define DISPLAYITEMDELEGATE_H +#ifndef ITEMDELEGATE_H +#define ITEMDELEGATE_H #include DWIDGET_USE_NAMESPACE -class DisplayItemDelegate : public DStyledItemDelegate +class ItemDelegate : public DStyledItemDelegate { public: static inline Qt::Alignment visualAlignment(Qt::LayoutDirection direction, Qt::Alignment alignment) @@ -26,7 +26,7 @@ class DisplayItemDelegate : public DStyledItemDelegate static QPixmap getIconPixmap(const QIcon &icon, const QSize &size, qreal pixelRatio, QIcon::Mode mode = QIcon::Normal, QIcon::State state = QIcon::Off); - explicit DisplayItemDelegate(QAbstractItemView *parent = nullptr); + explicit ItemDelegate(QAbstractItemView *parent = nullptr); protected: virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, @@ -43,4 +43,4 @@ class DisplayItemDelegate : public DStyledItemDelegate const QModelIndex &index, const QRectF &iconRect) const; }; -#endif // DISPLAYITEMDELEGATE_H +#endif // ITEMDELEGATE_H diff --git a/src/plugins/recent/mainframe/itemlistview.cpp b/src/plugins/recent/mainframe/itemlistview.cpp new file mode 100644 index 000000000..5eb21fce1 --- /dev/null +++ b/src/plugins/recent/mainframe/itemlistview.cpp @@ -0,0 +1,67 @@ +// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "itemlistview.h" +#include "common/common.h" +#include "itemdelegate.h" + +#include +#include + +#include + +#include +#include +#include +#include +#include + +#define INIT_DATA "{\n \"Projects\":[],\n \"Documents\":[]\n}\n" + +ItemListView::ItemListView(QWidget *parent) + : DListView(parent), model(new QStandardItemModel(this)) +{ + setDragDropMode(QAbstractItemView::NoDragDrop); + setSelectionMode(QAbstractItemView::SingleSelection); + setSelectionBehavior(QAbstractItemView::SelectRows); + setEditTriggers(QListView::NoEditTriggers); + setTextElideMode(Qt::ElideMiddle); + setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setAlternatingRowColors(false); + setSelectionRectVisible(true); + setMouseTracking(true); + + setUniformItemSizes(true); + setResizeMode(Fixed); + setOrientation(QListView::TopToBottom, false); + setFrameStyle(QFrame::NoFrame); + setSpacing(0); + setContentsMargins(0, 0, 0, 0); + + setModel(model); + setItemDelegate(new ItemDelegate(this)); +} + +ItemListView::~ItemListView() +{ +} + +void ItemListView::clear() +{ + model->clear(); +} + +void ItemListView::focusOutEvent(QFocusEvent *e) +{ + DListView::clearSelection(); + DListView::focusOutEvent(e); +} + +void ItemListView::mousePressEvent(QMouseEvent *e) +{ + if (e->button() != Qt::LeftButton) + return; + return DListView::mousePressEvent(e); +} diff --git a/src/plugins/recent/mainframe/itemlistview.h b/src/plugins/recent/mainframe/itemlistview.h new file mode 100644 index 000000000..8caa08398 --- /dev/null +++ b/src/plugins/recent/mainframe/itemlistview.h @@ -0,0 +1,37 @@ +// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef ITEMLISTVIEW_H +#define ITEMLISTVIEW_H + +#include +#include + +DWIDGET_USE_NAMESPACE + +class QStandardItemModel; +class QStandardItem; +class ItemListView : public DListView +{ + Q_OBJECT +public: + explicit ItemListView(QWidget *parent = nullptr); + ~ItemListView() override; + + virtual QString title() = 0; + virtual QString configKey() = 0; + virtual void setItemList(const QVariantList &items) = 0; + virtual QVariantList itemList() const = 0; + virtual void appendItem(const QVariant &item) = 0; + virtual void prependItem(const QVariant &item) = 0; + virtual void clear(); + + void focusOutEvent(QFocusEvent *e) override; + void mousePressEvent(QMouseEvent *e) override; + +protected: + QStandardItemModel *model { nullptr }; +}; + +#endif // ITEMLISTVIEW_H diff --git a/src/plugins/recent/mainframe/recentdisplay.cpp b/src/plugins/recent/mainframe/recentdisplay.cpp deleted file mode 100644 index 099fba94c..000000000 --- a/src/plugins/recent/mainframe/recentdisplay.cpp +++ /dev/null @@ -1,576 +0,0 @@ -// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. -// -// SPDX-License-Identifier: GPL-3.0-or-later - -#include "recentdisplay.h" -#include "displayrecentview.h" -#include "services/window/windowservice.h" -#include "services/project/projectservice.h" -#include "services/project/projectgenerator.h" - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -DWIDGET_USE_NAMESPACE -using namespace dpfservice; - -static RecentDisplay *ins{nullptr}; - -class DisplayProjectView : public DisplayRecentView -{ - QJsonArray projects; -public: - explicit DisplayProjectView(QWidget *parent = nullptr) - : DisplayRecentView(parent) - { - load(); - } - - virtual QString title() override - { - return tr("Projects"); - } - - virtual QList itemsFromFile() override - { - QJsonDocument doc = readRecent(); - QJsonObject obj = doc.object(); - QJsonArray array = obj.value(title()).toArray(); - QList result; - QStringList paths = cachedWorkspaces(projects); - for (auto val : array) { - auto elemObj = val.toObject(); - QString language, workspace, kitName; - QString file = projectFile(elemObj, &kitName, &language, &workspace); - if (file.isEmpty() || !QFileInfo(file).exists()) - continue; - auto rowItem = new DStandardItem (icon(file), file); - rowItem->setData(kitName, RecentDisplay::ProjectKitName); - rowItem->setData(language, RecentDisplay::ProjectLanguage); - rowItem->setData(workspace, RecentDisplay::ProjectWorkspace); - rowItem->setToolTip( "KitName: " + kitName + "\n" + - "Language: " + language + "\n" + - "Workspace: " + workspace ); - if (!paths.contains(file)) - projects << val; - result << rowItem; - } - return result; - } - - virtual void load() override - { - model->appendColumn(itemsFromFile()); - } - - virtual void add(const QString &kitName, - const QString &language, - const QString &workspace) - { - model->clear(); //删除数据 - auto paths = cachedWorkspaces(projects); - for (auto path : paths) { - if (!QFileInfo(path).exists()) { - removeProjectElem(projects, path); - } - } - if (paths.contains(workspace)) { - removeProjectElem(projects, workspace); - } - projects.insert(0, projectElem(kitName, language, workspace)); //置顶 - saveToFile(projects); //保存序列 - load(); //重新加载文件 - } - - virtual QString projectFile(const QJsonObject &elem, - QString *kitName = nullptr, - QString *language = nullptr, - QString *workspace = nullptr) - { - if (elem.keys().size() != 1) - return {}; - - QString file = elem.keys()[0]; - - if (file.isEmpty()) - return {}; - - auto propertyObj = elem.value(file).toObject(); - if (language) - *kitName = propertyObj["KitName"].toString(); - - if (language) - *language = propertyObj["Language"].toString(); - - if (workspace) - *workspace = propertyObj["Workspace"].toString(); - - return file; - } - - virtual void clearData() - { - QJsonArray emptyArr; - projects.swap(emptyArr); - model->clear(); - } - - virtual QJsonObject projectElem(const QString &kitName, - const QString &language, - const QString &workspace) - { - QJsonObject elem; - QJsonObject propertyVal; - propertyVal.insert("KitName", kitName); - propertyVal.insert("Language", language); - propertyVal.insert("Workspace", workspace); - elem.insert(workspace, QJsonValue{propertyVal}); - return elem; - } - - virtual QStringList cachedWorkspaces(const QJsonArray &array) - { - QStringList list; - for (auto val : array){ - list += val.toObject().keys(); - } - return list; - } - - virtual void removeProjectElem(QJsonArray &array, const QString &path) - { - if (path.isEmpty()) - return; - - for (int i = 0; i < array.size(); i++) { - auto keys = array[i].toObject().keys(); - if (keys.size() != 1) - continue; - - QString currPath = keys[0]; - if (currPath.isEmpty()) { - return; - } - if (currPath == path) { - array.removeAt(i); - } - } - } - - virtual void saveToFile(const QJsonArray &array) - { - QJsonDocument doc = readRecent(); - QJsonObject docObj = doc.object(); - docObj[title()] = array; - doc.setObject(docObj); - - QFile file(cachePath()); - if (file.open(QFile::WriteOnly)) { - file.write(doc.toJson()); - file.close(); - } - } -}; - -class DisplayDocView : public DisplayRecentView -{ -public: - explicit DisplayDocView(QWidget *parent = nullptr) - : DisplayRecentView(parent){ - load(); - } - - virtual QString title() override - { - return tr("Documents"); - } -}; - -class RecentDisplayPrivate -{ - friend class RecentDisplay; - QHBoxLayout *hLayout{nullptr}; - QVBoxLayout *vLayoutDoc{nullptr}; - QVBoxLayout *vLayoutPro{nullptr}; - DWidget *recentOpen{nullptr}; - DisplayProjectView *proView{nullptr}; - DisplayDocView *docView{nullptr}; - DLabel *proLabel{nullptr}; - DToolButton *proClear{nullptr}; - DDialog *clearProConfirm{nullptr}; - - DLabel *docLabel{nullptr}; - DToolButton *docClear{nullptr}; - DDialog *clearDocConfirm{nullptr}; - - DFrame *navFrame{nullptr}; - DFrame *docFrame{nullptr}; - DFrame *proFrame{nullptr}; - DPushButton *btnOpenFile{nullptr}; - DPushButton *btnOpenProject{nullptr}; - DPushButton *btnNewFileOrPro{nullptr}; - DLabel *nullRecentText{nullptr}; -}; - -RecentDisplay::RecentDisplay(DWidget *parent) - : DWidget (parent) - , d(new RecentDisplayPrivate()) -{ - initializeUi(); - initConnect(); -} - -RecentDisplay::~RecentDisplay() -{ - if (d) { - delete d; - } -} - -RecentDisplay *RecentDisplay::instance() -{ - if (!ins) - ins = new RecentDisplay; - return ins; -} - -void RecentDisplay::addDocument(const QString &filePath) -{ - d->docView->add(filePath); -} - -void RecentDisplay::addProject(const QString &kitName, - const QString &language, - const QString &workspace) -{ - d->proView->add(kitName, language, workspace); -} - -void RecentDisplay::doDoubleClickedProject(const QModelIndex &index) -{ - QString kitName = index.data(ProjectKitName).toString(); - QString language = index.data(ProjectLanguage).toString(); - QString workspace = index.data(ProjectWorkspace).toString(); - // "kitName", "language", "workspace" - project.openProject(kitName, language, workspace); - RecentDisplay::addProject(kitName, language, workspace); -} - -void RecentDisplay::doDoubleCliekedDocument(const QModelIndex &index) -{ - QString filePath = index.data(Qt::DisplayRole).toString(); - RecentDisplay::addDocument(filePath); - editor.openFile(QString(), filePath); -} - -void RecentDisplay::btnOpenFileClicked() -{ - QString dir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); - QString filePath = DFileDialog::getOpenFileName(nullptr, tr("Open Document"), dir); - if (filePath.isEmpty() && !QFileInfo(filePath).exists()) - return; - recent.saveOpenedFile(filePath); - editor.openFile(QString(), filePath); - - d->docView->load(); -} - -void RecentDisplay::btnOpenProjectClicked() -{ - auto projectService = dpfGetService(ProjectService); - projectService->openProject(); - d->proView->load(); -} - -void RecentDisplay::btnNewFileOrProClicked() -{ - projectTemplate.newWizard(); -} - -bool RecentDisplay::isProAndDocNull() -{ - QString cachePath = CustomPaths::user(CustomPaths::Configures) + QDir::separator() + "recent.support"; - QFile file(cachePath); - QJsonDocument recentFile; - - if (!file.exists()) - return true; - - if (file.open(QFile::ReadOnly)) { - recentFile = QJsonDocument::fromJson(file.readAll()); - if (recentFile["Projects"].isNull() && recentFile["Documents"].isNull()) - return true; - return recentFile["Projects"].toArray().isEmpty() && recentFile["Documents"].toArray().isEmpty(); - } - - return false; -} - -void RecentDisplay::clearProList() -{ - QString cachePath = CustomPaths::user(CustomPaths::Configures) + QDir::separator() + "recent.support"; - QFile file(cachePath); - QJsonDocument recentFile; - - if (!file.exists()) - return; - - if (file.open(QFile::ReadWrite)) { - recentFile = QJsonDocument::fromJson(file.readAll()); - if (recentFile["Projects"].isNull()) { - qWarning() << "no projects, clear Projects failed"; - return; - } - - QJsonArray emptyArr; - QJsonObject jsonObject = recentFile.object(); - jsonObject["Projects"] = emptyArr; - recentFile.setObject(jsonObject); - - file.seek(0); - file.write(recentFile.toJson(QJsonDocument::Indented)); - file.resize(file.pos()); - - d->proView->clearData(); - d->proView->load(); - } - - if (isProAndDocNull()) { - d->nullRecentText->setVisible(true); - d->recentOpen->setVisible(false); - } -} - -void RecentDisplay::clearDocList() -{ - QString cachePath = CustomPaths::user(CustomPaths::Configures) + QDir::separator() + "recent.support"; - QFile file(cachePath); - QJsonDocument recentFile; - - if (!file.exists()) - return; - - if (file.open(QFile::ReadWrite)) { - recentFile = QJsonDocument::fromJson(file.readAll()); - if (recentFile["Documents"].isNull()) { - qWarning() << "no Documents, clear Projects failed"; - return; - } - - QJsonArray emptyArr; - QJsonObject jsonObject = recentFile.object(); - jsonObject["Documents"] = emptyArr; - recentFile.setObject(jsonObject); - - file.seek(0); - file.write(recentFile.toJson(QJsonDocument::Indented)); - file.resize(file.pos()); - - d->docView->clearData(); - d->docView->load(); - } - - if (isProAndDocNull()) { - d->nullRecentText->setVisible(true); - d->recentOpen->setVisible(false); - } -} - -void RecentDisplay::initializeUi() -{ - d->navFrame = new DFrame(); - d->docFrame = new DFrame(); - d->proFrame = new DFrame(); - - d->navFrame->setLineWidth(0); - d->navFrame->setContentsMargins(0, 0, 0, 0); - DStyle::setFrameRadius(d->navFrame, 0); - QVBoxLayout *vLayoutNav = new QVBoxLayout(); - QLabel *recentLogo = new QLabel(); - QPixmap logo = QIcon(":/recent/images/recentLogo.png").pixmap(200, 143); - logo.setDevicePixelRatio(recentLogo->devicePixelRatioF()); - recentLogo->setPixmap(logo); - - d->btnOpenFile = new DPushButton(tr("Open File")); - d->btnOpenProject = new DPushButton(tr("Open Project")); - d->btnNewFileOrPro = new DPushButton(tr("New File or Project")); - d->nullRecentText = new DLabel(tr("No Project")); - d->nullRecentText->setAlignment(Qt::AlignCenter); - vLayoutNav->setContentsMargins(60, 0, 60, 0); - vLayoutNav->setSpacing(20); - vLayoutNav->setAlignment(Qt::AlignCenter); - vLayoutNav->addWidget(recentLogo); - vLayoutNav->addWidget(d->nullRecentText); - vLayoutNav->addWidget(d->btnOpenFile); - vLayoutNav->addWidget(d->btnOpenProject); - vLayoutNav->addWidget(d->btnNewFileOrPro); - d->navFrame->setLayout(vLayoutNav); - - DLabel *recentTitle = new DLabel(tr("Recent Open")); - recentTitle->setForegroundRole(QPalette::BrightText); - recentTitle->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); - DFontSizeManager::instance()->bind(recentTitle, DFontSizeManager::T4, QFont::Medium); - - //recent open document - d->docFrame->setLineWidth(0); - DStyle::setFrameRadius(d->docFrame, 0); - d->docView = new DisplayDocView(this); - - d->docLabel = new DLabel(tr("Documents")); - d->docLabel->setForegroundRole(QPalette::BrightText); - d->docLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); - d->docLabel->setContentsMargins(10, 5, 0, 10); - - d->docClear = new DToolButton(this); - d->docClear->setIcon(QIcon::fromTheme("ide_recent_delete")); - d->docClear->setToolTip(tr("clear all")); - - d->clearDocConfirm = new DDialog(this); - d->clearDocConfirm->setIcon(QIcon::fromTheme("dialog-warning")); - d->clearDocConfirm->setMessage(tr("Confirm to clear the record of the opened file?")); - d->clearDocConfirm->insertButton(0, tr("Cancel", "button")); - d->clearDocConfirm->insertButton(1, tr("Delete", "button"), true, DDialog::ButtonWarning); - - QHBoxLayout *docHlayout = new QHBoxLayout; - docHlayout->addWidget(d->docLabel); - docHlayout->addWidget(d->docClear); - - DFontSizeManager::instance()->bind(d->docLabel, DFontSizeManager::T4, QFont::Medium); - d->vLayoutDoc = new QVBoxLayout(); - d->vLayoutDoc->setContentsMargins(10, 10, 10, 10); - d->vLayoutDoc->addLayout(docHlayout); - d->vLayoutDoc->setSpacing(0); - d->vLayoutDoc->addWidget(d->docView); - d->docFrame->setLayout(d->vLayoutDoc); - - //recent open projects - d->proFrame->setLineWidth(0); - DStyle::setFrameRadius(d->proFrame, 0); - d->proView = new DisplayProjectView(this); - - d->proLabel = new DLabel(tr("Projects"), this); - d->proLabel->setForegroundRole(QPalette::BrightText); - d->proLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); - d->proLabel->setContentsMargins(10, 5, 0, 10); - - d->proClear = new DToolButton(this); - d->proClear->setIcon(QIcon::fromTheme("ide_recent_delete")); - d->proClear->setToolTip(tr("clear all")); - - d->clearProConfirm = new DDialog(this); - d->clearProConfirm->setIcon(QIcon::fromTheme("dialog-warning")); - d->clearProConfirm->setMessage(tr("Confirm to clear the record of the opened project?")); - d->clearProConfirm->insertButton(0, tr("Cancel", "button")); - d->clearProConfirm->insertButton(1, tr("Delete", "button"), true, DDialog::ButtonWarning); - - QHBoxLayout *proHlayout = new QHBoxLayout; - proHlayout->addWidget(d->proLabel); - proHlayout->addWidget(d->proClear); - - DFontSizeManager::instance()->bind(d->proLabel, DFontSizeManager::T4, QFont::Medium); - d->vLayoutPro = new QVBoxLayout(); - d->vLayoutPro->setContentsMargins(10, 10, 10, 10); - d->vLayoutPro->addLayout(proHlayout); - d->vLayoutPro->setSpacing(0); - d->vLayoutPro->addWidget(d->proView); - d->proFrame->setLayout(d->vLayoutPro); - - QHBoxLayout *proAndDocLayout = new QHBoxLayout(); - proAndDocLayout->addWidget(d->proFrame); - proAndDocLayout->setSpacing(2); - proAndDocLayout->addWidget(d->docFrame); - - d->recentOpen = new DWidget(this); - QVBoxLayout *recentNavLayout = new QVBoxLayout(d->recentOpen); - recentNavLayout->setContentsMargins(15, 0, 25, 20); - recentNavLayout->addSpacing(25); - recentNavLayout->setAlignment(Qt::AlignTop); - recentNavLayout->addWidget(recentTitle); - recentNavLayout->addSpacing(5); - recentNavLayout->addLayout(proAndDocLayout); - - d->hLayout = new QHBoxLayout(this); - d->hLayout->setContentsMargins(0, 0, 0, 0); - - d->hLayout->addWidget(d->navFrame); - d->hLayout->addWidget(d->recentOpen); - - if (!isProAndDocNull()) { - d->nullRecentText->setVisible(false); - d->recentOpen->setVisible(true); - } - -} - -void RecentDisplay::initConnect() -{ - QObject::connect(d->proView, &QListView::doubleClicked, - this, &RecentDisplay::doDoubleClickedProject, - Qt::UniqueConnection); - - QObject::connect(d->docView, &QListView::doubleClicked, - this, &RecentDisplay::doDoubleCliekedDocument, - Qt::UniqueConnection); - - QObject::connect(d->btnOpenFile, &DPushButton::clicked, - this, &RecentDisplay::btnOpenFileClicked, - Qt::UniqueConnection); - - QObject::connect(d->btnOpenProject, &DPushButton::clicked, - this, &RecentDisplay::btnOpenProjectClicked, - Qt::UniqueConnection); - - QObject::connect(d->btnNewFileOrPro, &DPushButton::clicked, - this, &RecentDisplay::btnNewFileOrProClicked, - Qt::UniqueConnection); - - QObject::connect(d->docClear, &DPushButton::clicked, - d->clearDocConfirm, &DDialog::exec, - Qt::UniqueConnection); - - QObject::connect(d->proClear, &DPushButton::clicked, - d->clearProConfirm, &DDialog::exec, - Qt::UniqueConnection); - - QObject::connect(d->clearDocConfirm, &DDialog::buttonClicked, this, [=](int index) { - if (index == 0) - d->clearDocConfirm->reject(); - else if (index == 1) - clearDocList(); - }); - - QObject::connect(d->clearProConfirm, &DDialog::buttonClicked, this, [=](int index) { - if (index == 0) - d->clearProConfirm->reject(); - else if (index == 1) - clearProList(); - }); -} - -void RecentDisplay::showEvent(QShowEvent *event) -{ - if(!isProAndDocNull()) { - d->nullRecentText->setVisible(false); - d->recentOpen->setVisible(true); - } else { - d->nullRecentText->setVisible(true); - d->recentOpen->setVisible(false); - } -} - - diff --git a/src/plugins/recent/mainframe/recentdisplaywidget.cpp b/src/plugins/recent/mainframe/recentdisplaywidget.cpp new file mode 100644 index 000000000..60fa429ad --- /dev/null +++ b/src/plugins/recent/mainframe/recentdisplaywidget.cpp @@ -0,0 +1,416 @@ +// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "recentdisplaywidget.h" +#include "recentlistview.h" +#include "services/window/windowservice.h" +#include "services/project/projectservice.h" +#include "services/project/projectgenerator.h" +#include "common/settings/settings.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +DWIDGET_USE_NAMESPACE +using namespace dpfservice; + +constexpr char kRecentGroup[] { "Recent" }; + +static RecentDisplayWidget *ins { nullptr }; + +class RecentDisplayWidgetPrivate +{ + friend class RecentDisplayWidget; + QHBoxLayout *hLayout { nullptr }; + QVBoxLayout *vLayoutDoc { nullptr }; + QVBoxLayout *vLayoutPro { nullptr }; + DWidget *recentOpen { nullptr }; + RecentProjectView *proView { nullptr }; + RecentDocemntView *docView { nullptr }; + QList viewList; + DLabel *proLabel { nullptr }; + DToolButton *proClear { nullptr }; + DDialog *clearProConfirm { nullptr }; + + DLabel *docLabel { nullptr }; + DToolButton *docClear { nullptr }; + DDialog *clearDocConfirm { nullptr }; + + DFrame *navFrame { nullptr }; + DFrame *docFrame { nullptr }; + DFrame *proFrame { nullptr }; + DPushButton *btnOpenFile { nullptr }; + DPushButton *btnOpenProject { nullptr }; + DPushButton *btnNewFileOrPro { nullptr }; + DLabel *nullRecentText { nullptr }; + + Settings recentSettings; +}; + +RecentDisplayWidget::RecentDisplayWidget(DWidget *parent) + : DWidget(parent), d(new RecentDisplayWidgetPrivate()) +{ + initializeUi(); + initConnect(); + initData(); +} + +RecentDisplayWidget::~RecentDisplayWidget() +{ + d->recentSettings.setValue(kRecentGroup, d->proView->configKey(), d->proView->itemList()); + d->recentSettings.setValue(kRecentGroup, d->docView->configKey(), d->docView->itemList()); + + delete d; +} + +RecentDisplayWidget *RecentDisplayWidget::instance() +{ + if (!ins) + ins = new RecentDisplayWidget; + return ins; +} + +void RecentDisplayWidget::addDocument(const QString &filePath) +{ + auto itemList = d->docView->itemList(); + if (itemList.contains(filePath)) + itemList.removeOne(filePath); + itemList.prepend(filePath); + + d->docView->clear(); + d->docView->setItemList(itemList); +} + +void RecentDisplayWidget::addProject(const QString &kitName, + const QString &language, + const QString &workspace) +{ + QVariantMap item; + item.insert(kKitName, kitName); + item.insert(kLanguage, language); + item.insert(kWorkspace, workspace); + + auto itemList = d->proView->itemList(); + if (itemList.contains(item)) + itemList.removeOne(item); + itemList.prepend(item); + + d->proView->clear(); + d->proView->setItemList(itemList); +} + +void RecentDisplayWidget::doDoubleClickedProject(const QModelIndex &index) +{ + QString kitName = index.data(RecentProjectView::KitNameRole).toString(); + QString language = index.data(RecentProjectView::LanguageRole).toString(); + QString workspace = index.data(RecentProjectView::WorkspaceRole).toString(); + // "kitName", "language", "workspace" + project.openProject(kitName, language, workspace); + RecentDisplayWidget::addProject(kitName, language, workspace); +} + +void RecentDisplayWidget::doDoubleCliekedDocument(const QModelIndex &index) +{ + QString filePath = index.data(Qt::DisplayRole).toString(); + RecentDisplayWidget::addDocument(filePath); + editor.openFile(QString(), filePath); +} + +void RecentDisplayWidget::btnOpenFileClicked() +{ + QString dir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); + QString filePath = DFileDialog::getOpenFileName(nullptr, tr("Open Document"), dir); + if (filePath.isEmpty() && !QFileInfo(filePath).exists()) + return; + recent.saveOpenedFile(filePath); + editor.openFile(QString(), filePath); +} + +void RecentDisplayWidget::btnOpenProjectClicked() +{ + auto projectService = dpfGetService(ProjectService); + projectService->openProject(); +} + +void RecentDisplayWidget::btnNewFileOrProClicked() +{ + projectTemplate.newWizard(); +} + +bool RecentDisplayWidget::isProAndDocNull() +{ + return d->proView->itemList().isEmpty() && d->docView->itemList().isEmpty(); +} + +QVariantMap RecentDisplayWidget::parseProjectInfo(const QJsonObject &obj) +{ + if (obj.keys().size() != 1) + return {}; + + QString file = obj.keys()[0]; + if (file.isEmpty()) + return {}; + + QVariantMap map; + auto propertyObj = obj.value(file).toObject(); + map.insert(kKitName, propertyObj["KitName"].toString()); + map.insert(kLanguage, propertyObj["Language"].toString()); + map.insert(kWorkspace, propertyObj["Workspace"].toString()); + + return map; +} + +void RecentDisplayWidget::clearProList() +{ + d->proView->clear(); + d->recentSettings.removeGroup(d->proView->configKey()); + + if (isProAndDocNull()) { + d->nullRecentText->setVisible(true); + d->recentOpen->setVisible(false); + } +} + +void RecentDisplayWidget::clearDocList() +{ + d->docView->clear(); + d->recentSettings.removeGroup(d->docView->configKey()); + + if (isProAndDocNull()) { + d->nullRecentText->setVisible(true); + d->recentOpen->setVisible(false); + } +} + +void RecentDisplayWidget::initializeUi() +{ + d->navFrame = new DFrame(); + d->docFrame = new DFrame(); + d->proFrame = new DFrame(); + + d->navFrame->setLineWidth(0); + d->navFrame->setContentsMargins(0, 0, 0, 0); + DStyle::setFrameRadius(d->navFrame, 0); + QVBoxLayout *vLayoutNav = new QVBoxLayout(); + QLabel *recentLogo = new QLabel(); + QPixmap logo = QIcon(":/recent/images/recentLogo.png").pixmap(200, 143); + logo.setDevicePixelRatio(recentLogo->devicePixelRatioF()); + recentLogo->setPixmap(logo); + + d->btnOpenFile = new DPushButton(tr("Open File")); + d->btnOpenProject = new DPushButton(tr("Open Project")); + d->btnNewFileOrPro = new DPushButton(tr("New File or Project")); + d->nullRecentText = new DLabel(tr("No Project")); + d->nullRecentText->setAlignment(Qt::AlignCenter); + vLayoutNav->setContentsMargins(60, 0, 60, 0); + vLayoutNav->setSpacing(20); + vLayoutNav->setAlignment(Qt::AlignCenter); + vLayoutNav->addWidget(recentLogo); + vLayoutNav->addWidget(d->nullRecentText); + vLayoutNav->addWidget(d->btnOpenFile); + vLayoutNav->addWidget(d->btnOpenProject); + vLayoutNav->addWidget(d->btnNewFileOrPro); + d->navFrame->setLayout(vLayoutNav); + + DLabel *recentTitle = new DLabel(tr("Recent Open")); + recentTitle->setForegroundRole(QPalette::BrightText); + recentTitle->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); + DFontSizeManager::instance()->bind(recentTitle, DFontSizeManager::T4, QFont::Medium); + + //recent open document + d->docFrame->setLineWidth(0); + DStyle::setFrameRadius(d->docFrame, 0); + d->docView = new RecentDocemntView(this); + + d->docLabel = new DLabel(tr("Documents")); + d->docLabel->setForegroundRole(QPalette::BrightText); + d->docLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); + d->docLabel->setContentsMargins(10, 5, 0, 10); + + d->docClear = new DToolButton(this); + d->docClear->setIcon(QIcon::fromTheme("ide_recent_delete")); + d->docClear->setToolTip(tr("clear all")); + + d->clearDocConfirm = new DDialog(this); + d->clearDocConfirm->setIcon(QIcon::fromTheme("dialog-warning")); + d->clearDocConfirm->setMessage(tr("Confirm to clear the record of the opened file?")); + d->clearDocConfirm->insertButton(0, tr("Cancel", "button")); + d->clearDocConfirm->insertButton(1, tr("Delete", "button"), true, DDialog::ButtonWarning); + + QHBoxLayout *docHlayout = new QHBoxLayout; + docHlayout->addWidget(d->docLabel); + docHlayout->addWidget(d->docClear); + + DFontSizeManager::instance()->bind(d->docLabel, DFontSizeManager::T4, QFont::Medium); + d->vLayoutDoc = new QVBoxLayout(); + d->vLayoutDoc->setContentsMargins(10, 10, 10, 10); + d->vLayoutDoc->addLayout(docHlayout); + d->vLayoutDoc->setSpacing(0); + d->vLayoutDoc->addWidget(d->docView); + d->docFrame->setLayout(d->vLayoutDoc); + + //recent open projects + d->proFrame->setLineWidth(0); + DStyle::setFrameRadius(d->proFrame, 0); + d->proView = new RecentProjectView(this); + + d->proLabel = new DLabel(tr("Projects"), this); + d->proLabel->setForegroundRole(QPalette::BrightText); + d->proLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); + d->proLabel->setContentsMargins(10, 5, 0, 10); + + d->proClear = new DToolButton(this); + d->proClear->setIcon(QIcon::fromTheme("ide_recent_delete")); + d->proClear->setToolTip(tr("clear all")); + + d->clearProConfirm = new DDialog(this); + d->clearProConfirm->setIcon(QIcon::fromTheme("dialog-warning")); + d->clearProConfirm->setMessage(tr("Confirm to clear the record of the opened project?")); + d->clearProConfirm->insertButton(0, tr("Cancel", "button")); + d->clearProConfirm->insertButton(1, tr("Delete", "button"), true, DDialog::ButtonWarning); + + QHBoxLayout *proHlayout = new QHBoxLayout; + proHlayout->addWidget(d->proLabel); + proHlayout->addWidget(d->proClear); + + DFontSizeManager::instance()->bind(d->proLabel, DFontSizeManager::T4, QFont::Medium); + d->vLayoutPro = new QVBoxLayout(); + d->vLayoutPro->setContentsMargins(10, 10, 10, 10); + d->vLayoutPro->addLayout(proHlayout); + d->vLayoutPro->setSpacing(0); + d->vLayoutPro->addWidget(d->proView); + d->proFrame->setLayout(d->vLayoutPro); + + QHBoxLayout *proAndDocLayout = new QHBoxLayout(); + proAndDocLayout->addWidget(d->proFrame); + proAndDocLayout->setSpacing(2); + proAndDocLayout->addWidget(d->docFrame); + + d->recentOpen = new DWidget(this); + QVBoxLayout *recentNavLayout = new QVBoxLayout(d->recentOpen); + recentNavLayout->setContentsMargins(15, 0, 25, 20); + recentNavLayout->addSpacing(25); + recentNavLayout->setAlignment(Qt::AlignTop); + recentNavLayout->addWidget(recentTitle); + recentNavLayout->addSpacing(5); + recentNavLayout->addLayout(proAndDocLayout); + + d->hLayout = new QHBoxLayout(this); + d->hLayout->setContentsMargins(0, 0, 0, 0); + + d->hLayout->addWidget(d->navFrame); + d->hLayout->addWidget(d->recentOpen); + + if (!isProAndDocNull()) { + d->nullRecentText->setVisible(false); + d->recentOpen->setVisible(true); + } +} + +void RecentDisplayWidget::initConnect() +{ + QObject::connect(d->proView, &QListView::doubleClicked, + this, &RecentDisplayWidget::doDoubleClickedProject, + Qt::UniqueConnection); + + QObject::connect(d->docView, &QListView::doubleClicked, + this, &RecentDisplayWidget::doDoubleCliekedDocument, + Qt::UniqueConnection); + + QObject::connect(d->btnOpenFile, &DPushButton::clicked, + this, &RecentDisplayWidget::btnOpenFileClicked, + Qt::UniqueConnection); + + QObject::connect(d->btnOpenProject, &DPushButton::clicked, + this, &RecentDisplayWidget::btnOpenProjectClicked, + Qt::UniqueConnection); + + QObject::connect(d->btnNewFileOrPro, &DPushButton::clicked, + this, &RecentDisplayWidget::btnNewFileOrProClicked, + Qt::UniqueConnection); + + QObject::connect(d->docClear, &DPushButton::clicked, + d->clearDocConfirm, &DDialog::exec, + Qt::UniqueConnection); + + QObject::connect(d->proClear, &DPushButton::clicked, + d->clearProConfirm, &DDialog::exec, + Qt::UniqueConnection); + + QObject::connect(d->clearDocConfirm, &DDialog::buttonClicked, this, [=](int index) { + if (index == 0) + d->clearDocConfirm->reject(); + else if (index == 1) + clearDocList(); + }); + + QObject::connect(d->clearProConfirm, &DDialog::buttonClicked, this, [=](int index) { + if (index == 0) + d->clearProConfirm->reject(); + else if (index == 1) + clearProList(); + }); +} + +[[deprecated("-------------存在兼容代码需要删除")]] void RecentDisplayWidget::initData() +{ + const QString oldCfgFile = CustomPaths::user(CustomPaths::Configures) + QDir::separator() + "recent.support"; + const QString newCfgFile = CustomPaths::user(CustomPaths::Configures) + QDir::separator() + "recent.json"; + d->recentSettings.load("", newCfgFile); + + QFile file(oldCfgFile); + if (file.open(QIODevice::ReadOnly)) { + auto doc = QJsonDocument::fromJson(file.readAll()); + file.close(); + file.remove(); + + QVariantList prjList; + QJsonArray prjArray = doc.object().value(d->proView->configKey()).toArray(); + for (const auto &prj : prjArray) { + auto prjObj = prj.toObject(); + const auto &info = parseProjectInfo(prjObj); + if (!info.isEmpty()) + prjList << info; + } + d->recentSettings.setValue(kRecentGroup, d->proView->configKey(), prjList); + + QVariantList docList; + QJsonArray docArray = doc.object().value(d->docView->configKey()).toArray(); + for (const auto &doc : docArray) { + auto filePath = doc.toString(); + if (!filePath.isEmpty()) + docList << filePath; + } + d->recentSettings.setValue(kRecentGroup, d->docView->configKey(), docList); + } + + d->proView->setItemList(d->recentSettings.value(kRecentGroup, d->proView->configKey()).toList()); + d->docView->setItemList(d->recentSettings.value(kRecentGroup, d->docView->configKey()).toList()); +} + +void RecentDisplayWidget::showEvent(QShowEvent *event) +{ + if (!isProAndDocNull()) { + d->nullRecentText->setVisible(false); + d->recentOpen->setVisible(true); + } else { + d->nullRecentText->setVisible(true); + d->recentOpen->setVisible(false); + } +} diff --git a/src/plugins/recent/mainframe/recentdisplay.h b/src/plugins/recent/mainframe/recentdisplaywidget.h similarity index 62% rename from src/plugins/recent/mainframe/recentdisplay.h rename to src/plugins/recent/mainframe/recentdisplaywidget.h index 70b08b2c5..5a9467300 100644 --- a/src/plugins/recent/mainframe/recentdisplay.h +++ b/src/plugins/recent/mainframe/recentdisplaywidget.h @@ -2,26 +2,22 @@ // // SPDX-License-Identifier: GPL-3.0-or-later -#ifndef RECENTDISPLAY_H -#define RECENTDISPLAY_H +#ifndef RECENTDISPLAYWIDGET_H +#define RECENTDISPLAYWIDGET_H #include #include "common/common.h" -class RecentDisplayPrivate; -class RecentDisplay : public DTK_WIDGET_NAMESPACE::DWidget +class RecentDisplayWidgetPrivate; +class RecentDisplayWidget : public DTK_WIDGET_NAMESPACE::DWidget { Q_OBJECT - RecentDisplayPrivate *const d; + RecentDisplayWidgetPrivate *const d; + public: - enum ItemRole{ - ProjectKitName = Qt::ItemDataRole::UserRole, - ProjectLanguage, - ProjectWorkspace, - }; - explicit RecentDisplay(DTK_WIDGET_NAMESPACE::DWidget *parent = nullptr); - virtual ~RecentDisplay() override; - static RecentDisplay *instance(); + explicit RecentDisplayWidget(DTK_WIDGET_NAMESPACE::DWidget *parent = nullptr); + virtual ~RecentDisplayWidget() override; + static RecentDisplayWidget *instance(); public slots: void addDocument(const QString &filePath); @@ -39,12 +35,15 @@ private slots: void clearDocList(); void clearProList(); + private: void initializeUi(); void initConnect(); + void initData(); bool isProAndDocNull(); + QVariantMap parseProjectInfo(const QJsonObject &obj); void showEvent(QShowEvent *event) override; }; -#endif // RECENTDISPLAY_H +#endif // RECENTDISPLAYWIDGET_H diff --git a/src/plugins/recent/mainframe/recentlistview.cpp b/src/plugins/recent/mainframe/recentlistview.cpp new file mode 100644 index 000000000..4d8b4ef4e --- /dev/null +++ b/src/plugins/recent/mainframe/recentlistview.cpp @@ -0,0 +1,140 @@ +// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "recentlistview.h" + +#include + +static QIcon icon(const QString &file) +{ + static DFileIconProvider provider; + return provider.icon(QFileInfo(file)); +} + +RecentProjectView::RecentProjectView(QWidget *parent) + : ItemListView(parent) +{ +} + +QString RecentProjectView::title() +{ + return tr("Projects"); +} + +QString RecentProjectView::configKey() +{ + return "Projects"; +} + +void RecentProjectView::setItemList(const QVariantList &items) +{ + clear(); + for (const auto &item : items) { + appendItem(item); + } +} + +QVariantList RecentProjectView::itemList() const +{ + QVariantList itemList; + for (int i = 0; i < model->rowCount(); ++i) { + QVariantMap map; + const auto &item = model->item(i); + map.insert(kKitName, item->data(KitNameRole)); + map.insert(kLanguage, item->data(LanguageRole)); + map.insert(kWorkspace, item->data(WorkspaceRole)); + + itemList << map; + } + + return itemList; +} + +void RecentProjectView::appendItem(const QVariant &item) +{ + if (!item.canConvert(QMetaType::QVariantMap)) + return; + + model->appendRow(createItem(item)); +} + +void RecentProjectView::prependItem(const QVariant &item) +{ + if (!item.canConvert(QMetaType::QVariantMap)) + return; + + model->insertRow(0, createItem(item)); +} + +DStandardItem *RecentProjectView::createItem(const QVariant &item) +{ + const auto &infoMap = item.toMap(); + const auto kitName = infoMap.value(kKitName).toString(); + const auto language = infoMap.value(kLanguage).toString(); + const auto workspace = infoMap.value(kWorkspace).toString(); + + auto rowItem = new DStandardItem(icon(workspace), workspace); + rowItem->setData(kitName, KitNameRole); + rowItem->setData(language, LanguageRole); + rowItem->setData(workspace, WorkspaceRole); + QString tipFormat("KitName: %1\nLanguage: %2\nWorkspace: %3"); + rowItem->setToolTip(tipFormat.arg(kitName, language, workspace)); + + return rowItem; +} + +RecentDocemntView::RecentDocemntView(QWidget *parent) + : ItemListView(parent) +{ +} + +QString RecentDocemntView::title() +{ + return tr("Documents"); +} + +QString RecentDocemntView::configKey() +{ + return "Documents"; +} + +void RecentDocemntView::setItemList(const QVariantList &items) +{ + clear(); + for (const auto &item : items) { + appendItem(item); + } +} + +QVariantList RecentDocemntView::itemList() const +{ + QVariantList itemList; + for (int i = 0; i < model->rowCount(); ++i) { + const auto &item = model->item(i); + const auto filePath = item->data(Qt::DisplayRole); + itemList << filePath; + } + + return itemList; +} + +void RecentDocemntView::appendItem(const QVariant &item) +{ + if (!item.canConvert(QMetaType::QString)) + return; + + QString filePath = item.toString(); + auto rowItem = new DStandardItem(icon(filePath), filePath); + model->appendRow(rowItem); +} + +void RecentDocemntView::prependItem(const QVariant &item) +{ + if (!item.canConvert(QMetaType::QString)) + return; + + QString filePath = item.toString(); + auto rowItem = new DStandardItem(icon(filePath), filePath); + model->insertRow(0, rowItem); +} diff --git a/src/plugins/recent/mainframe/recentlistview.h b/src/plugins/recent/mainframe/recentlistview.h new file mode 100644 index 000000000..1d6948193 --- /dev/null +++ b/src/plugins/recent/mainframe/recentlistview.h @@ -0,0 +1,51 @@ +// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RECENTLISTVIEW_H +#define RECENTLISTVIEW_H + +#include "itemlistview.h" + +constexpr char kKitName[] { "KitName" }; +constexpr char kLanguage[] { "Language" }; +constexpr char kWorkspace[] { "Workspace" }; + +class RecentProjectView : public ItemListView +{ + Q_OBJECT +public: + enum ItemRole { + KitNameRole = Qt::ItemDataRole::UserRole, + LanguageRole, + WorkspaceRole + }; + + RecentProjectView(QWidget *parent = nullptr); + + QString title() override; + QString configKey() override; + void setItemList(const QVariantList &items) override; + QVariantList itemList() const override; + void appendItem(const QVariant &item) override; + void prependItem(const QVariant &item) override; + +private: + DStandardItem *createItem(const QVariant &item); +}; + +class RecentDocemntView : public ItemListView +{ + Q_OBJECT +public: + RecentDocemntView(QWidget *parent = nullptr); + + QString title() override; + QString configKey() override; + void setItemList(const QVariantList &items) override; + QVariantList itemList() const override; + void appendItem(const QVariant &item) override; + void prependItem(const QVariant &item) override; +}; + +#endif // RECENTLISTVIEW_H diff --git a/src/plugins/recent/recent.cpp b/src/plugins/recent/recent.cpp index cd43a72ce..dc80ad623 100644 --- a/src/plugins/recent/recent.cpp +++ b/src/plugins/recent/recent.cpp @@ -7,7 +7,7 @@ #include "base/abstractaction.h" #include "base/abstractwidget.h" #include "services/window/windowservice.h" -#include "mainframe/recentdisplay.h" +#include "mainframe/recentdisplaywidget.h" #include "transceiver/recentreceiver.h" #include @@ -28,15 +28,15 @@ bool Recent::start() if (windowService) { QObject::connect(RecentProxy::instance(), &RecentProxy::saveOpenedProject, - RecentDisplay::instance(), &RecentDisplay::addProject); + RecentDisplayWidget::instance(), &RecentDisplayWidget::addProject); QObject::connect(RecentProxy::instance(), &RecentProxy::saveOpenedFile, - RecentDisplay::instance(), &RecentDisplay::addDocument); + RecentDisplayWidget::instance(), &RecentDisplayWidget::addDocument); QAction *action = new QAction(MWNA_RECENT, this); action->setIcon(QIcon::fromTheme("recent-navigation")); windowService->addNavigationItem(new AbstractAction(action), Priority::highest); - auto recentWidgetImpl = new AbstractWidget(RecentDisplay::instance()); + auto recentWidgetImpl = new AbstractWidget(RecentDisplayWidget::instance()); windowService->registerWidgetToMode("recentWindow", recentWidgetImpl, CM_RECENT, Position::FullWindow, true, true); } return true;