| 1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "plugindialog.h" |
| 6 | |
| 7 | #include "framework/lifecycle/lifecycle.h" |
| 8 | #include "framework/lifecycle/pluginmanager.h" |
| 9 | #include "framework/lifecycle/pluginmetaobject.h" |
| 10 | #include "framework/lifecycle/pluginview.h" |
| 11 | |
| 12 | #include <QHBoxLayout> |
| 13 | #include <QVBoxLayout> |
| 14 | #include <QDialog> |
| 15 | #include <QPushButton> |
| 16 | #include <QLabel> |
| 17 | #include <QDialogButtonBox> |
| 18 | #include <QDesktopServices> |
| 19 | |
| 20 | static bool isRestartRequired = false; |
| 21 | |
| 22 | PluginDialog::PluginDialog(QWidget *parent) |
| 23 | : QDialog(parent), |
| 24 | view(new dpf::PluginView(this)) |
| 25 | { |
| 26 | resize(1000, 600); |
| 27 | setWindowTitle(tr("Installed Plugins" )); |
| 28 | |
| 29 | auto vLayout = new QVBoxLayout(this); |
| 30 | auto detailLayout = new QHBoxLayout; |
| 31 | detailLayout->addWidget(view); |
| 32 | |
| 33 | closeButton = new QDialogButtonBox(QDialogButtonBox::Close, Qt::Horizontal, this); |
| 34 | closeButton->button(QDialogButtonBox::Close)->setText(tr("Close" )); |
| 35 | closeButton->setEnabled(true); |
| 36 | |
| 37 | restratRequired = new QLabel(tr(" Restart required." ), this); |
| 38 | if (!isRestartRequired) |
| 39 | restratRequired->setVisible(false); |
| 40 | |
| 41 | auto buttonLayout = new QHBoxLayout; |
| 42 | buttonLayout->addSpacing(10); |
| 43 | buttonLayout->addWidget(restratRequired); |
| 44 | buttonLayout->addStretch(5); |
| 45 | buttonLayout->addWidget(closeButton); |
| 46 | |
| 47 | detailView = new DetailsView(); |
| 48 | detailLayout->addWidget(detailView); |
| 49 | slotCurrentPluginActived(); |
| 50 | |
| 51 | vLayout->addLayout(detailLayout); |
| 52 | vLayout->addLayout(buttonLayout); |
| 53 | |
| 54 | QObject::connect(view, &dpf::PluginView::currentPluginActived, this, &PluginDialog::slotCurrentPluginActived); |
| 55 | QObject::connect(view, &dpf::PluginView::pluginSettingChanged, |
| 56 | this, &PluginDialog::updateRestartRequired); |
| 57 | QObject::connect(closeButton->button(QDialogButtonBox::Close), &QPushButton::clicked, |
| 58 | [this] { closeDialog() ;}); |
| 59 | } |
| 60 | |
| 61 | void PluginDialog::slotCurrentPluginActived() |
| 62 | { |
| 63 | dpf::PluginMetaObjectPointer plugin = view->currentPlugin(); |
| 64 | detailView->update(plugin); |
| 65 | } |
| 66 | |
| 67 | void PluginDialog::updateRestartRequired() |
| 68 | { |
| 69 | isRestartRequired = true; |
| 70 | restratRequired->setVisible(true); |
| 71 | } |
| 72 | |
| 73 | void PluginDialog::closeDialog() |
| 74 | { |
| 75 | dpf::LifeCycle::getPluginManagerInstance()->writeSettings(); |
| 76 | accept(); |
| 77 | } |
| 78 | |
| 79 | DetailsView::DetailsView(QWidget *parent) |
| 80 | : QWidget(parent) |
| 81 | { |
| 82 | gridLayout = new QGridLayout(this); |
| 83 | gridLayout->setContentsMargins(2, 2, 2, 2); |
| 84 | |
| 85 | nameLabel = new QLabel(tr("Name:" ), this); |
| 86 | nameLabel->setTextInteractionFlags(Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse); |
| 87 | name = new QLabel(this); |
| 88 | name->setTextInteractionFlags(Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse); |
| 89 | gridLayout->addWidget(nameLabel, 0, 0, 1, 1); |
| 90 | gridLayout->addWidget(name, 0, 1, 1, 1); |
| 91 | |
| 92 | versionLabel = new QLabel(tr("Version:" ), this); |
| 93 | versionLabel->setTextInteractionFlags(Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse); |
| 94 | version = new QLabel(this); |
| 95 | version->setTextInteractionFlags(Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse); |
| 96 | gridLayout->addWidget(versionLabel, 1, 0, 1, 1); |
| 97 | gridLayout->addWidget(version, 1, 1, 1, 1); |
| 98 | |
| 99 | compatVersionLabel = new QLabel(tr("Compatibility version:" ), this); |
| 100 | compatVersionLabel->setTextInteractionFlags(Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse); |
| 101 | compatVersion = new QLabel(this); |
| 102 | compatVersion->setTextInteractionFlags(Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse); |
| 103 | gridLayout->addWidget(compatVersionLabel, 2, 0, 1, 1); |
| 104 | gridLayout->addWidget(compatVersion, 2, 1, 1, 1); |
| 105 | |
| 106 | vendorLabel = new QLabel(tr("Vendor:" ), this); |
| 107 | vendorLabel->setTextInteractionFlags(Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse); |
| 108 | vendor = new QLabel(this); |
| 109 | vendor->setTextInteractionFlags(Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse); |
| 110 | gridLayout->addWidget(vendorLabel, 3, 0, 1, 1); |
| 111 | gridLayout->addWidget(vendor, 3, 1, 1, 1); |
| 112 | |
| 113 | copyrightLabel = new QLabel(tr("Copyright:" ), this); |
| 114 | copyrightLabel->setTextInteractionFlags(Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse); |
| 115 | copyright = new QLabel(this); |
| 116 | copyright->setTextInteractionFlags(Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse); |
| 117 | gridLayout->addWidget(copyrightLabel, 4, 0, 1, 1); |
| 118 | gridLayout->addWidget(copyright, 4, 1, 1, 1); |
| 119 | |
| 120 | categoryLabel = new QLabel(tr("Category:" ), this); |
| 121 | categoryLabel->setTextInteractionFlags(Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse); |
| 122 | category = new QLabel(this); |
| 123 | category->setTextInteractionFlags(Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse); |
| 124 | gridLayout->addWidget(categoryLabel, 5, 0, 1, 1); |
| 125 | gridLayout->addWidget(category, 5, 1, 1, 1); |
| 126 | |
| 127 | urlLabel = new QLabel(tr("URL:" ), this); |
| 128 | urlLabel->setTextInteractionFlags(Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse); |
| 129 | urlLink = new QLabel(this); |
| 130 | urlLink->setTextInteractionFlags(Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse); |
| 131 | gridLayout->addWidget(urlLabel, 6, 0, 1, 1); |
| 132 | gridLayout->addWidget(urlLink, 6, 1, 1, 1); |
| 133 | |
| 134 | vboxLayout_1 = new QVBoxLayout(); |
| 135 | licenseLabel = new QLabel(tr("License:" ), this); |
| 136 | licenseLabel->setTextInteractionFlags(Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse); |
| 137 | vboxLayout_1->addWidget(licenseLabel); |
| 138 | spacerItem_1 = new QSpacerItem(17, 13, QSizePolicy::Minimum, QSizePolicy::Expanding); |
| 139 | vboxLayout_1->addItem(spacerItem_1); |
| 140 | license = new QTextEdit(this); |
| 141 | license->setReadOnly(true); |
| 142 | gridLayout->addLayout(vboxLayout_1, 7, 0, 1, 1); |
| 143 | gridLayout->addWidget(license, 7, 1, 1, 1); |
| 144 | |
| 145 | vboxLayout_2 = new QVBoxLayout(); |
| 146 | descriptionLabel = new QLabel(tr("Description:" ), this); |
| 147 | descriptionLabel->setTextInteractionFlags(Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse); |
| 148 | vboxLayout_2->addWidget(descriptionLabel); |
| 149 | spacerItem_2 = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding); |
| 150 | vboxLayout_2->addItem(spacerItem_2); |
| 151 | description = new QTextEdit(this); |
| 152 | description->setReadOnly(true); |
| 153 | gridLayout->addLayout(vboxLayout_2, 8, 0, 1, 1); |
| 154 | gridLayout->addWidget(description, 8, 1, 1, 1); |
| 155 | |
| 156 | vboxLayout_3 = new QVBoxLayout(); |
| 157 | dependenciesLabel = new QLabel(tr("Dependencies:" ), this); |
| 158 | dependenciesLabel->setTextInteractionFlags(Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse); |
| 159 | vboxLayout_3->addWidget(dependenciesLabel); |
| 160 | spacerItem_3 = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding); |
| 161 | vboxLayout_3->addItem(spacerItem_3); |
| 162 | dependencies = new QListWidget(this); |
| 163 | gridLayout->addLayout(vboxLayout_3, 9, 0, 1, 1); |
| 164 | gridLayout->addWidget(dependencies, 9, 1, 1, 1); |
| 165 | } |
| 166 | |
| 167 | void DetailsView::update(dpf::PluginMetaObjectPointer plugin) |
| 168 | { |
| 169 | name->setText(plugin->name()); |
| 170 | version->setText(plugin->version()); |
| 171 | compatVersion->setText(plugin->compatVersion()); |
| 172 | vendor->setText(plugin->vendor()); |
| 173 | category->setText(plugin->category()); |
| 174 | const QString link = QString::fromLatin1("<a href=\"%1\">%1</a>" ).arg(plugin->urlLink()); |
| 175 | urlLink->setText(link); |
| 176 | copyright->setText(plugin->copyright()); |
| 177 | description->setText(plugin->description()); |
| 178 | QString licenseText; |
| 179 | for (QString &text : plugin->license()) { |
| 180 | licenseText += text.simplified(); |
| 181 | licenseText += "\n\n" ; |
| 182 | } |
| 183 | license->setText(licenseText.trimmed()); |
| 184 | QStringList dependsList; |
| 185 | dependencies->clear(); |
| 186 | for (dpf::PluginDepend depend : plugin->depends()) { |
| 187 | dependsList << depend.toString(); |
| 188 | } |
| 189 | dependencies->addItems(dependsList); |
| 190 | |
| 191 | connect(urlLink, &QLabel::linkActivated, [=](){ |
| 192 | QDesktopServices::openUrl(QUrl(plugin->urlLink())); |
| 193 | }); |
| 194 | } |
| 195 | |