xref: /OK3568_Linux_fs/app/QLauncher/desktopwindow.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 **   * Redistributions of source code must retain the above copyright
25 **     notice, this list of conditions and the following disclaimer.
26 **   * Redistributions in binary form must reproduce the above copyright
27 **     notice, this list of conditions and the following disclaimer in
28 **     the documentation and/or other materials provided with the
29 **     distribution.
30 **   * Neither the name of The Qt Company Ltd nor the names of its
31 **     contributors may be used to endorse or promote products derived
32 **     from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 #include "desktopwindow.h"
51 #include "xdgdesktopfile.h"
52 #include <QAbstractScrollArea>
53 
54 #define DESKTOP_DIR "/usr/share/applications"
55 
56 #define ICON_SIZE 128
57 #define ITEM_SPACE ICON_SIZE / 2
58 #define FONT_SIZE ICON_SIZE / 6
DesktopWindow()59 DesktopWindow::DesktopWindow()
60 {
61     QDesktopWidget *desktopwidget = QApplication::desktop();
62     QRect desktoprect = desktopwidget->availableGeometry();
63     qDebug() << "QLauncher available size :" << desktoprect.width() << "x" << desktoprect.height();
64 
65     QDir dir(DESKTOP_DIR);
66     QStringList filters;
67     filters << "*.desktop";
68     dir.setNameFilters(filters);
69     list = dir.entryInfoList();
70 
71     if (list.length()!=0) {
72         QListWidget *desktopList = new QListWidget();
73         for (int i = 0; i < list.size(); ++i) {
74             XdgDesktopFile df;
75             df.load(list.at(i).fileName());
76             QListWidgetItem *item = new QListWidgetItem(df.icon(ICON_SIZE), df.name());
77 //            qDebug() << "QLauncher add application:" << i << df.name();
78             QFont font;
79             font.setPixelSize(FONT_SIZE);
80             item->setFont(font);
81             item->setSizeHint(QSize(ICON_SIZE + ITEM_SPACE, ICON_SIZE + FONT_SIZE + ITEM_SPACE));
82             desktopList->addItem(item);
83         }
84         desktopList->setSpacing(ITEM_SPACE);
85         desktopList->setViewMode(QListView::IconMode);
86         desktopList->setFlow(QListView::LeftToRight);
87         desktopList->setDragEnabled(false);
88         desktopList->setWordWrap(true);
89         desktopList->setWrapping(true);
90         desktopList->setFrameShape(QListWidget::NoFrame);
91         desktopList->setGridSize(QSize(ICON_SIZE + ITEM_SPACE, ICON_SIZE + ITEM_SPACE));
92         desktopList->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
93         desktopList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
94         desktopList->setStyleSheet("background-color:transparent");
95         desktopList->setIconSize(QSize(ICON_SIZE, ICON_SIZE));
96         desktopList->setResizeMode(QListWidget::Adjust);
97         setCentralWidget(desktopList);
98         setWindowState(Qt::WindowMaximized);
99         setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnBottomHint);
100         setStyleSheet("QListWidget{background-color:transparent}");
101         QProcess p;
102         p.start("cat /etc/os-release");
103         p.waitForStarted();
104         p.waitForFinished();
105         QTextStream tt(p.readAllStandardOutput());
106         QString ll;
107         bool found = 0;
108         do{
109             ll = tt.readLine();
110 //            qDebug() << ll;
111             if(!ll.compare("NAME=\"Debian GNU/Linux\"")){
112                 found = true;
113             }
114         }while (! ll.isNull());
115         if(found)
116             setStyleSheet("QMainWindow{border-image: url(:/resources/background_debian.jpg);}");
117         else
118             setStyleSheet("QMainWindow{border-image: url(:/resources/background_linux.jpg);}");
119         connect(desktopList, SIGNAL(itemPressed(QListWidgetItem *)), this, SLOT(on_itemClicked(QListWidgetItem *)));
120     } else
121         qDebug()<<"QLauncher no found .desktop file in"<<DESKTOP_DIR;
122         resize(QGuiApplication::primaryScreen()->availableSize());
123 }
124 
on_itemClicked(QListWidgetItem * item)125 void DesktopWindow::on_itemClicked(QListWidgetItem * item)
126 {
127     for (int i = 0; i < list.size(); ++i) {
128         XdgDesktopFile df;
129         df.load(list.at(i).fileName());
130         if (df.name() == item->text()) {
131             qDebug()<<"QLauncher start"<<df.name();
132             df.startDetached();
133         }
134     }
135 }
136 
137 
138