mainwindow.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. #include "mainwindow.h"
  2. void MainWindow::readConfig() {
  3. QString configPath = QDir::homePath() + "/.config/plainDE/config.json";
  4. if (!QFile::exists(configPath)) {
  5. qDebug() << "~/.config/plainDE/config.json does not exist. "
  6. "Generating new config...";
  7. system("python3 /usr/share/plainDE/tools/genconfig.py");
  8. }
  9. QFile file(configPath);
  10. file.open(QIODevice::ReadOnly | QIODevice::Text);
  11. QString data = file.readAll();
  12. file.close();
  13. mCfgObj = QJsonDocument::fromJson(data.toUtf8()).object();
  14. }
  15. void MainWindow::setAppearance() {
  16. QIcon::setThemeName("Mint-L-Grey");
  17. mFont.setFamily(mCfgObj["fontFamily"].toString());
  18. mFont.setPointSize(mCfgObj["fontSize"].toInt());
  19. qDebug() << mFont;
  20. mTitleFont = mFont;
  21. mTitleFont.setBold(true);
  22. this->setFont(mFont);
  23. QString themeName = mCfgObj["theme"].toString();
  24. QString stylesheetPath = QString("/usr/share/plainDE/styles/%1").arg(themeName);
  25. QFile stylesheetReader(stylesheetPath);
  26. stylesheetReader.open(QIODevice::ReadOnly | QIODevice::Text);
  27. QTextStream styleSheet(&stylesheetReader);
  28. this->setStyleSheet(styleSheet.readAll());
  29. stylesheetReader.close();
  30. }
  31. void MainWindow::createUI(QApplication* app) {
  32. this->setObjectName("controlCenter");
  33. this->setWindowTitle("plainControlCenter");
  34. QString theme = mCfgObj["theme"].toString();
  35. mMainLayout = new QVBoxLayout(this);
  36. mMainLayout->setContentsMargins(0, 0, 0, 0);
  37. mMainLayout->setSpacing(0);
  38. QWidget* toolbarWidget = new QWidget();
  39. QHBoxLayout* toolbarLayout = new QHBoxLayout(toolbarWidget);
  40. toolbarLayout->setContentsMargins(5, 5, 5, 5);
  41. toolbarLayout->setSpacing(6);
  42. QWidget* buttonsWidget = new QWidget();
  43. QHBoxLayout* buttonsLayout = new QHBoxLayout(buttonsWidget);
  44. buttonsLayout->setContentsMargins(0, 0, 0, 0);
  45. buttonsLayout->setSpacing(0);
  46. QPushButton* backPushButton = new QPushButton("◀"); // U+25C0 - Left-Pointing Triangle
  47. backPushButton->setMinimumSize(30, 25);
  48. backPushButton->setMaximumSize(30, 25);
  49. buttonsLayout->addWidget(backPushButton);
  50. QPushButton* forwardPushButton = new QPushButton("▶"); // U+25B6 - Right-Pointing Triangle
  51. forwardPushButton->setMinimumSize(30, 25);
  52. forwardPushButton->setMaximumSize(30, 25);
  53. buttonsLayout->addWidget(forwardPushButton);
  54. toolbarLayout->addWidget(buttonsWidget);
  55. toolbarLayout->addSpacerItem(new QSpacerItem(0, 0,
  56. QSizePolicy::MinimumExpanding,
  57. QSizePolicy::Ignored));
  58. QLineEdit* searchBox = new QLineEdit();
  59. searchBox->setClearButtonEnabled(true);
  60. searchBox->setPlaceholderText("🔎 " + tr("Search")); // U+01F50E - magnifier icon
  61. QHash<QString, QString> dataByEntry;
  62. QFile searchDBReader("/usr/share/plainDE/pCC-searchdb.json");
  63. searchDBReader.open(QIODevice::Text | QIODevice::ReadOnly);
  64. QString searchDB = searchDBReader.readAll();
  65. searchDBReader.close();
  66. mSearchDBObj = QJsonDocument::fromJson(searchDB.toUtf8()).object();
  67. QCompleter* completer = new QCompleter(this);
  68. QStandardItemModel* model = new QStandardItemModel(this);
  69. model->setRowCount(mSearchDBObj.count());
  70. int currentRow = 0;
  71. foreach (QString entry, mSearchDBObj.keys()) {
  72. QStringList data = mSearchDBObj[entry].toString().split(';');
  73. QStandardItem* item = new QStandardItem(entry);
  74. item->setFont(mFont);
  75. item->setIcon(QIcon::fromTheme(data[0]));
  76. model->setItem(currentRow, item);
  77. ++currentRow;
  78. }
  79. completer->setModel(model);
  80. completer->setCaseSensitivity(Qt::CaseInsensitive);
  81. searchBox->setCompleter(completer);
  82. connect(completer, SIGNAL(activated(QModelIndex)), this, SLOT(goTo(QModelIndex)));
  83. toolbarLayout->addWidget(searchBox);
  84. mMainLayout->addWidget(toolbarWidget);
  85. QWidget* contentsWidget = new QWidget();
  86. contentsWidget->setObjectName("contentsWidget");
  87. QVBoxLayout* contentsLayout = new QVBoxLayout(contentsWidget);
  88. contentsLayout->setContentsMargins(0, 0, 0, 0);
  89. contentsLayout->setSpacing(0);
  90. QFrame* personalFrame = new QFrame();
  91. personalFrame->setObjectName("controlCenterFrameA");
  92. personalFrame->setFont(mFont);
  93. if (!theme.contains("transparent")) {
  94. personalFrame->setFrameShape(QFrame::StyledPanel);
  95. personalFrame->setFrameShadow(QFrame::Raised);
  96. }
  97. else {
  98. personalFrame->setFrameShape(QFrame::NoFrame);
  99. }
  100. personalFrame->setFrameShape(QFrame::NoFrame);
  101. QVBoxLayout* personalLayout = new QVBoxLayout(personalFrame);
  102. QLabel* personalLabel = new QLabel(tr("Personal"));
  103. personalLabel->setFont(mTitleFont);
  104. personalLayout->addWidget(personalLabel);
  105. QHBoxLayout* personalButtonsLayout = new QHBoxLayout();
  106. personalButtonsLayout->setContentsMargins(20, 0, 0, 0);
  107. personalButtonsLayout->setSpacing(25);
  108. QToolButton* generalButton = new QToolButton();
  109. generalButton->setText(tr("General"));
  110. generalButton->setIcon(QIcon::fromTheme("cs-general"));
  111. generalButton->setIconSize(QSize(32, 32));
  112. generalButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
  113. generalButton->setAutoRaise(true);
  114. generalButton->setMinimumWidth(90);
  115. generalButton->setMaximumWidth(90);
  116. personalButtonsLayout->addWidget(generalButton);
  117. QToolButton* appearanceButton = new QToolButton();
  118. appearanceButton->setText(tr("Appearance"));
  119. appearanceButton->setIcon(QIcon::fromTheme("cs-themes"));
  120. appearanceButton->setIconSize(QSize(32, 32));
  121. appearanceButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
  122. appearanceButton->setAutoRaise(true);
  123. appearanceButton->setMinimumWidth(90);
  124. appearanceButton->setMaximumWidth(90);
  125. personalButtonsLayout->addWidget(appearanceButton);
  126. QToolButton* panelsButton = new QToolButton();
  127. panelsButton->setText(tr("Panels"));
  128. panelsButton->setIcon(QIcon::fromTheme("cs-applets"));
  129. panelsButton->setIconSize(QSize(32, 32));
  130. panelsButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
  131. panelsButton->setAutoRaise(true);
  132. panelsButton->setMinimumWidth(90);
  133. panelsButton->setMaximumWidth(90);
  134. personalButtonsLayout->addWidget(panelsButton);
  135. personalButtonsLayout->addSpacerItem(new QSpacerItem(0, 0,
  136. QSizePolicy::MinimumExpanding,
  137. QSizePolicy::Ignored));
  138. personalLayout->addLayout(personalButtonsLayout);
  139. contentsLayout->addWidget(personalFrame);
  140. QFrame* hardwareFrame = new QFrame();
  141. hardwareFrame->setObjectName("controlCenterFrameB");
  142. hardwareFrame->setFont(mFont);
  143. if (!theme.contains("transparent")) {
  144. hardwareFrame->setFrameShape(QFrame::StyledPanel);
  145. hardwareFrame->setFrameShadow(QFrame::Raised);
  146. }
  147. else {
  148. hardwareFrame->setFrameShape(QFrame::NoFrame);
  149. }
  150. hardwareFrame->setFrameShape(QFrame::NoFrame);
  151. QVBoxLayout* hardwareLayout = new QVBoxLayout(hardwareFrame);
  152. QLabel* hardwareLabel = new QLabel(tr("Hardware"));
  153. hardwareLabel->setFont(mTitleFont);
  154. hardwareLayout->addWidget(hardwareLabel);
  155. QHBoxLayout* hardwareButtonsLayout = new QHBoxLayout(hardwareFrame);
  156. hardwareButtonsLayout->setContentsMargins(20, 0, 0, 0);
  157. hardwareButtonsLayout->setSpacing(25);
  158. QToolButton* keyboardButton = new QToolButton();
  159. keyboardButton->setText(tr("Keyboard"));
  160. keyboardButton->setIcon(QIcon::fromTheme("cs-keyboard"));
  161. keyboardButton->setIconSize(QSize(32, 32));
  162. keyboardButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
  163. keyboardButton->setAutoRaise(true);
  164. keyboardButton->setMinimumWidth(90);
  165. keyboardButton->setMaximumWidth(90);
  166. hardwareButtonsLayout->addWidget(keyboardButton);
  167. hardwareButtonsLayout->addSpacerItem(new QSpacerItem(0, 0,
  168. QSizePolicy::MinimumExpanding,
  169. QSizePolicy::Ignored));
  170. hardwareLayout->addLayout(hardwareButtonsLayout);
  171. contentsLayout->addWidget(hardwareFrame);
  172. QFrame* systemFrame = new QFrame();
  173. systemFrame->setObjectName("controlCenterFrameA");
  174. systemFrame->setFont(mFont);
  175. if (!theme.contains("transparent")) {
  176. systemFrame->setFrameShape(QFrame::StyledPanel);
  177. systemFrame->setFrameShadow(QFrame::Raised);
  178. }
  179. else {
  180. systemFrame->setFrameShape(QFrame::NoFrame);
  181. }
  182. systemFrame->setFrameShape(QFrame::NoFrame);
  183. QVBoxLayout* systemLayout = new QVBoxLayout(systemFrame);
  184. QLabel* systemLabel = new QLabel(tr("System"));
  185. systemLabel->setFont(mTitleFont);
  186. systemLayout->addWidget(systemLabel);
  187. QHBoxLayout* systemButtonsLayout = new QHBoxLayout(systemFrame);
  188. systemButtonsLayout->setContentsMargins(20, 0, 0, 0);
  189. systemButtonsLayout->setSpacing(25);
  190. QToolButton* autostartButton = new QToolButton();
  191. autostartButton->setText(tr("Autostart"));
  192. autostartButton->setIcon(QIcon::fromTheme("cs-general"));
  193. autostartButton->setIconSize(QSize(32, 32));
  194. autostartButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
  195. autostartButton->setAutoRaise(true);
  196. autostartButton->setMinimumWidth(90);
  197. autostartButton->setMaximumWidth(90);
  198. systemButtonsLayout->addWidget(autostartButton);
  199. QToolButton* aboutButton = new QToolButton();
  200. aboutButton->setText(tr("About") + "\nplainDE");
  201. aboutButton->setIcon(QIcon("/usr/share/plainDE/menuIcon.png"));
  202. aboutButton->setIconSize(QSize(32, 32));
  203. aboutButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
  204. aboutButton->setAutoRaise(true);
  205. aboutButton->setMinimumWidth(90);
  206. aboutButton->setMaximumWidth(90);
  207. systemButtonsLayout->addWidget(aboutButton);
  208. systemButtonsLayout->addSpacerItem(new QSpacerItem(0, 0,
  209. QSizePolicy::MinimumExpanding,
  210. QSizePolicy::Ignored));
  211. systemLayout->addLayout(systemButtonsLayout);
  212. contentsLayout->addWidget(systemFrame);
  213. contentsLayout->addSpacerItem(new QSpacerItem(0, 0,
  214. QSizePolicy::Ignored,
  215. QSizePolicy::MinimumExpanding));
  216. mMainLayout->addWidget(contentsWidget);
  217. QScreen* primaryScreen = app->primaryScreen();
  218. QRect screenGeometry = primaryScreen->geometry();
  219. int width = 525, height = 650;
  220. int x = screenGeometry.x() + ((screenGeometry.width() - width) / 2);
  221. int y = screenGeometry.y() + ((screenGeometry.height() - height) / 2);
  222. this->setGeometry(x, y, width, height);
  223. this->setFixedSize(width, height);
  224. mCurrentWidget = contentsWidget;
  225. mHistory.append(mCurrentWidget);
  226. // Making connections
  227. connect(generalButton, &QToolButton::clicked, this, [this]() {
  228. showPane("general");
  229. });
  230. connect(appearanceButton, &QToolButton::clicked, this, [this]() {
  231. showPane("appearance");
  232. });
  233. connect(keyboardButton, &QToolButton::clicked, this, [this]() {
  234. showPane("keyboard");
  235. });
  236. connect(autostartButton, &QToolButton::clicked, this, [this]() {
  237. showPane("autostart");
  238. });
  239. connect(panelsButton, &QToolButton::clicked, this, [this]() {
  240. showPane("panels");
  241. });
  242. connect(aboutButton, &QPushButton::clicked, this, [this]() {
  243. QProcess* process = new QProcess(this);
  244. process->start("plainAbout", {"--plainControlCenter"});
  245. });
  246. connect(backPushButton, &QPushButton::clicked, this, [this]() {
  247. if (mHistoryPointer > 0) {
  248. mCurrentWidget->setVisible(false);
  249. mCurrentWidget = mHistory.at(--mHistoryPointer);
  250. mCurrentWidget->setVisible(true);
  251. }
  252. qDebug() << mHistory << mHistoryPointer;
  253. });
  254. connect(forwardPushButton, &QPushButton::clicked, this, [this]() {
  255. if (mHistoryPointer < mHistory.length() - 1) {
  256. mCurrentWidget->setVisible(false);
  257. mCurrentWidget = mHistory.at(++mHistoryPointer);
  258. mCurrentWidget->setVisible(true);
  259. }
  260. qDebug() << mHistory << mHistoryPointer;
  261. });
  262. connect(searchBox, &QLineEdit::returnPressed, this, [this, searchBox]() {
  263. goTo(searchBox->text());
  264. });
  265. }
  266. void MainWindow::showPane(QString name) {
  267. mCurrentWidget->setVisible(false);
  268. int length = mHistory.length();
  269. for (int i = 0; i < length - mHistoryPointer - 1; ++i) {
  270. mHistory.removeLast();
  271. }
  272. Pane* pane;
  273. if (!name.compare("general")) {
  274. pane = new GeneralPane(&mCfgObj);
  275. }
  276. else if (!name.compare("appearance")) {
  277. pane = new AppearancePane(&mCfgObj, this);
  278. }
  279. else if (!name.compare("panels")) {
  280. pane = new PanelsPane(&mCfgObj);
  281. }
  282. else if (!name.compare("keyboard")) {
  283. pane = new KeyboardPane(&mCfgObj);
  284. }
  285. else if (!name.compare("autostart")) {
  286. pane = new AutostartPane(&mCfgObj);
  287. }
  288. pane->setPaneContents();
  289. mMainLayout->addWidget(pane);
  290. mHistory.append(pane);
  291. mCurrentWidget = pane;
  292. ++mHistoryPointer;
  293. }
  294. void MainWindow::showDialog(QString name) {
  295. Dialog* dialog;
  296. if (!name.compare("usermenu")) {
  297. dialog = new UserMenuDialog(&mCfgObj);
  298. }
  299. else if (!name.compare("datetime")) {
  300. dialog = new DateTimeDialog(&mCfgObj);
  301. }
  302. else if (!name.compare("volume")) {
  303. dialog = new VolumeDialog(&mCfgObj);
  304. }
  305. else if (!name.compare("appmenu")) {
  306. dialog = new AppMenuDialog(&mCfgObj);
  307. }
  308. else if (!name.compare("localipv4")) {
  309. dialog = new LocalIPv4Dialog(&mCfgObj);
  310. }
  311. else if (!name.compare("windowlist")) {
  312. dialog = new WindowListDialog(&mCfgObj);
  313. }
  314. else if (!name.compare("kblayout")) {
  315. dialog = new KbIndicatorDialog(&mCfgObj);
  316. }
  317. else if (!name.compare("workspaces")) {
  318. dialog = new WorkspacesDialog(&mCfgObj);
  319. }
  320. else if (!name.compare("windowlist")) {
  321. dialog = new WindowListDialog(&mCfgObj);
  322. }
  323. dialog->setPaneContents();
  324. dialog->show();
  325. }
  326. void MainWindow::goTo(QString entry) {
  327. if (mSearchDBObj.contains(entry)) {
  328. QStringList path = mSearchDBObj[entry].toString().split(';')[1].split("->");
  329. // Leg 1
  330. showPane(path.at(0));
  331. // Leg 2
  332. if (path.length() > 1) {
  333. if (!path.at(1).compare("panelX")) {
  334. // Leg 3
  335. QString applet = path.at(2);
  336. int panelID = 1;
  337. while (panelID <= 4) {
  338. QString panelName = QString("panel%1").arg(QString::number(panelID));
  339. QJsonArray applets = mCfgObj[panelName].toObject()["applets"].toArray();
  340. if (applets.contains(applet)) {
  341. break;
  342. }
  343. ++panelID;
  344. }
  345. showDialog(applet);
  346. }
  347. }
  348. }
  349. }
  350. void MainWindow::goTo(QModelIndex index) {
  351. QString entry = index.data().toString();
  352. goTo(entry);
  353. }
  354. void MainWindow::setTransparency() {
  355. QScreen* screen = this->screen();
  356. QRect widgetGeometry = this->geometry();
  357. QPixmap pixmap = screen->grabWindow(0,
  358. widgetGeometry.x(),
  359. widgetGeometry.y(),
  360. widgetGeometry.width(),
  361. widgetGeometry.height());
  362. QGraphicsBlurEffect* blurEffect = new QGraphicsBlurEffect();
  363. blurEffect->setBlurRadius(15);
  364. blurEffect->setBlurHints(QGraphicsBlurEffect::QualityHint);
  365. QGraphicsScene* scene = new QGraphicsScene();
  366. QGraphicsPixmapItem item;
  367. item.setPixmap(pixmap);
  368. item.setGraphicsEffect(blurEffect);
  369. scene->addItem(&item);
  370. QImage res(QSize(widgetGeometry.width(), widgetGeometry.height()), QImage::Format_ARGB32);
  371. res.fill(Qt::transparent);
  372. QPainter ptr(&res);
  373. scene->render(&ptr, QRectF(), QRectF(0, 0, widgetGeometry.width(), widgetGeometry.height()));
  374. QPalette palette;
  375. palette.setBrush(this->backgroundRole(),
  376. QBrush(QPixmap::fromImage(res)));
  377. this->setPalette(palette);
  378. }
  379. MainWindow::MainWindow(QWidget *parent,
  380. QApplication* app) : QWidget(parent) {
  381. readConfig();
  382. setAppearance();
  383. createUI(app);
  384. if (mCfgObj["theme"].toString().contains("transparent")) {
  385. setTransparency();
  386. }
  387. }
  388. MainWindow::~MainWindow() {
  389. }