1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
#include "mainwindow.h"
#include "pages/homepage.h"
#include "pages/updatespage.h"
#include "backend/settings.h"
#include "pages/categorypage.h"
#include <DTitlebar>
#include <DSearchEdit>
#include <DSettingsDialog>
#include <DDialog>
#include <DMenu>
#include <DCheckBox>
#include <DRadioButton>
#include <QHBoxLayout>
#include <QLabel>
MainWindow::MainWindow(QWidget *parent)
: DMainWindow(parent)
{
this->setMinimumSize(950, 600);
QWidget *centralWidget = new QWidget;
QHBoxLayout *layout = new QHBoxLayout;
layout->setContentsMargins(0, 0, 0, 0);
centralWidget->setLayout(layout);
setCentralWidget(centralWidget);
// Initialize updates page here so tray button works
UpdatesPage *updates = new UpdatesPage(this);
updatesPage = updates;
updateIndicator = new DViewItemAction(Qt::AlignVCenter, QSize(16, 16), QSize(16, 16), false);
updateIndicator->setIcon(QIcon("://icons/indicator.svg"));
updateIndicator->setVisible(false);
// Initialize tray item and buttons
trayIcon = new QSystemTrayIcon(QIcon::fromTheme("deepin-app-store"));
DMenu *trayIconMenu = new DMenu;
QAction *checkUpdatesAction = new QAction(tr("Check for updates"));
QAction *quitAction = new QAction(tr("Quit"));
trayIconMenu->addActions({checkUpdatesAction, quitAction});
trayIcon->setContextMenu(trayIconMenu);
trayIcon->setToolTip("DDE Store");
// Set up button actions
connect(quitAction, &QAction::triggered, this, &MainWindow::close);
connect(updates, &UpdatesPage::cantRefresh, this, [ = ] {
checkUpdatesAction->setDisabled(true);
});
connect(updates, &UpdatesPage::canRefresh, this, [ = ] {
checkUpdatesAction->setDisabled(false);
});
connect(checkUpdatesAction, &QAction::triggered, updates, [ = ] { updates->refresh(true); });
connect(trayIcon, &QSystemTrayIcon::activated, this, [ = ] {
show();
trayIcon->hide();
});
stackedWidget = new QStackedWidget(this);
// Initialize sidebar (navView) and titlebar
initTitlebar();
initNav();
layout->addWidget(navView, 1);
layout->addWidget(stackedWidget, 7);
}
void MainWindow::initTitlebar()
{
DTitlebar *titlebar = this->titlebar();
titlebar->setIcon(QIcon::fromTheme("deepin-app-store"));
// Create back button
backButton = new DButtonBoxButton(DStyle::SP_ArrowLeave);
backButton->setDisabled(true);
backButton->setFixedSize(36, 36);
connect(backButton, &DButtonBoxButton::clicked, this, [ = ] {
pageHistoryIndex -= 1;
buttonNavigated = true;
stackedWidget->setCurrentWidget(pageHistory[pageHistoryIndex]);
// If the new current page has an entry in the sidebar then select that entry
if (pageHistoryIndex < navModel->rowCount())
navView->setCurrentIndex(navModel->index(stackedWidget->indexOf(pageHistory[pageHistoryIndex]), 0));
});
// Create forward button
forwardButton = new DButtonBoxButton(DStyle::SP_ArrowEnter);
forwardButton->setDisabled(true);
forwardButton->setFixedSize(36, 36);
connect(forwardButton, &DButtonBoxButton::clicked, this, [ = ] {
// The same as back button, just increases the index rather than decreasing it
pageHistoryIndex += 1;
buttonNavigated = true;
stackedWidget->setCurrentWidget(pageHistory[pageHistoryIndex]);
if (pageHistoryIndex < navModel->rowCount())
navView->setCurrentIndex(navModel->index(stackedWidget->indexOf(pageHistory[pageHistoryIndex]), 0));
});
// Initialize the button box and add the buttons
DButtonBox *buttonBox = new DButtonBox(this);
buttonBox->setButtonList({backButton, forwardButton}, false);
buttonBox->setFocusPolicy(Qt::NoFocus);
titlebar->addWidget(buttonBox, Qt::AlignLeft);
// Create search box
DSearchEdit *searchBox = new DSearchEdit(this);
searchBox->setFixedWidth(300);
titlebar->addWidget(searchBox);
connect(searchBox, &DSearchEdit::returnPressed, this, [ = ] {
// Create a new category page for the search
CategoryPage *page = new CategoryPage(this, QString("\"%1\"").arg(searchBox->text()), searchBox->text());
// Open it
stackedWidget->addWidget(page);
stackedWidget->setCurrentWidget(page);
// Clear the sidebar selection
navView->setCurrentIndex(QModelIndex());
navView->clearSelection();
});
connect(searchBox, &DSearchEdit::searchAborted, this, [ = ] {
// Go to homepage when search aborted
navView->setCurrentIndex(navModel->index(0, 0));
stackedWidget->setCurrentIndex(0);
});
titlebar->setMenu(new QMenu(titlebar));
QAction *settingsAction = new QAction(tr("Settings"), this);
titlebar->menu()->addAction(settingsAction);
connect(titlebar->menu(), &QMenu::triggered, this, [ = ] (QAction *action) {
if (action == settingsAction) {
DSettingsDialog *dialog = new DSettingsDialog;
dialog->updateSettings(settings::instance()->appsettings);
dialog->exec();
}
});
}
void MainWindow::initNav()
{
// Set up list
navView->setViewportMargins(QMargins(10, 10, 10, 10));
navView->setMinimumWidth(188);
navView->setSpacing(0);
navView->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
navView->setIconSize(QSize(28, 28));
navView->setItemSize(QSize(168, 56));
navView->setAutoFillBackground(true);
navView->setModel(navModel);
navView->setEditTriggers(QListView::NoEditTriggers);
// Add entries
addPage(tr("Home"), "home.svg", new HomePage(this));
addPage(tr("Messaging"), "chat.svg", new CategoryPage(this, tr("Messaging"), "InstantMessaging"));
addPage(tr("Internet"), "internet.svg", new CategoryPage(this, tr("Internet"), "Network"));
addPage(tr("Games"), "games.svg", new CategoryPage(this, tr("Games"), "Game"));
addPage(tr("Development"), "development.svg", new CategoryPage(this, tr("Development"), "Development"));
addPage(tr("Office"), "office.svg", new CategoryPage(this, tr("Office"), "Office"));
addPage(tr("Graphics"), "graphics.svg", new CategoryPage(this, tr("Graphics"), "Graphics"));
addPage(tr("Video"), "video.svg", new CategoryPage(this, tr("Video"), "Video"));
addPage(tr("Music"), "music.svg", new CategoryPage(this, tr("Music"), "Music"));
addPage(tr("System"), "system.svg", new CategoryPage(this, tr("System"), "System"));
addPage(tr("Installed"), "installed.svg", new CategoryPage(this, tr("Installed"), "Installed"));
addPage(tr("Updates"), "updates.svg", updatesPage);
updateIcons();
// When the current entry is changed
connect(navView, qOverload<const QModelIndex &>(&DListView::currentChanged), this, [ = ] (const QModelIndex &previous) {
updateIcons();
if (navView->currentIndex().row() != -1)
stackedWidget->setCurrentIndex(navView->currentIndex().row());
if (!buttonNavigated) {
// Add the page to history and increase index by 1
pageHistory << stackedWidget->currentWidget();
pageHistoryIndex += 1;
// If the index isn't at the end of the history (the user pressed back)
if (pageHistoryIndex != pageHistory.length() - 1) {
// Remove all entries after this current one
for (int i = 0; i < pageHistory.length() - pageHistoryIndex + 1; i++) {
pageHistory.removeLast();
}
pageHistory << stackedWidget->currentWidget();
pageHistoryIndex = pageHistory.length() - 1;
}
} else {
buttonNavigated = false;
}
backButton->setDisabled(pageHistoryIndex < 1);
forwardButton->setDisabled(pageHistoryIndex == pageHistory.length() - 1);
});
// Select homepage by default
navView->setCurrentIndex(navModel->index(0, 0));
connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, &MainWindow::updateIcons);
}
void MainWindow::addPage(QString name, QString iconname, QWidget *widget)
{
DStandardItem *item = new DStandardItem(name);
navModel->appendRow(item);
pageIcons[name] = iconname;
stackedWidget->addWidget(widget);
if (qobject_cast<UpdatesPage*>(widget))
item->setActionList(Qt::Edge::RightEdge, {updateIndicator});
}
void MainWindow::openItem(App *app)
{
// If the item page is in the list
if (itemPageList.contains(app->id)) {
// Open it
stackedWidget->setCurrentWidget(itemPageList.value(app->id));
} else {
// If not, create the page and add it to the list
ItemPage *widget = new ItemPage(app);
stackedWidget->addWidget(widget);
stackedWidget->setCurrentWidget(widget);
itemPageList.insert(app->id, widget);
}
navView->setCurrentIndex(QModelIndex());
navView->clearSelection();
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if (!trayIcon->isVisible()) {
if (!settings::instance()->remembered()) {
DDialog dialog;
DRadioButton *exitButton = new DRadioButton(tr("Exit"));
exitButton->setChecked(!settings::instance()->tray());
DRadioButton *minimizeButton = new DRadioButton(tr("Minimize to system tray"));
minimizeButton->setChecked(settings::instance()->tray());
DCheckBox *rememberBox = new DCheckBox(tr("Do not ask again"));
dialog.setTitle(tr("Please choose your action"));
dialog.setIcon(QIcon::fromTheme("deepin-app-store"));
dialog.addContent(exitButton);
dialog.addContent(minimizeButton);
dialog.addContent(rememberBox);
dialog.addButton(tr("Cancel"), false, DDialog::ButtonNormal);
dialog.addButton(tr("Confirm"), true, DDialog::ButtonRecommend);
int index = dialog.exec();
if (index == 1) {
if (rememberBox->isChecked()) {
settings::instance()->setValue("basic.behaviour.remember", true);
}
if (exitButton->isChecked()) {
settings::instance()->setValue("basic.behaviour.tray", false);
} else if (minimizeButton->isChecked()) {
settings::instance()->setValue("basic.behaviour.tray", true);
event->ignore();
trayIcon->show();
hide();
}
} else {
event->ignore();
}
} else {
if (settings::instance()->tray()) {
event->ignore();
trayIcon->show();
hide();
}
}
}
if (SourceManager::instance()->preventingClose()) {
DDialog dialog;
dialog.setIcon(style()->standardIcon(QStyle::SP_MessageBoxCritical));
dialog.setTitle(tr("Cannot close while app is being installed"));
dialog.addButton(tr("OK"));
dialog.exec();
event->ignore();
}
}
void MainWindow::updateIcons()
{
auto type = DGuiApplicationHelper::instance()->themeType();
for (int i = 0; i < navModel->rowCount(); i++) {
QString icon = pageIcons.value(navModel->item(i)->text());
if (type == DGuiApplicationHelper::LightType)
icon = "://icons/" + QString(navView->currentIndex().row() == i ? "active/" : "light/") + icon;
else
icon = "://icons/dark/" + icon;
if (navModel->item(i)->icon().name() != icon)
navModel->item(i)->setIcon(QIcon(icon));
}
}
void MainWindow::setUpdateIndicatorVisible(bool value)
{
updateIndicator->setVisible(value);
}
|