~feng-kylin/youker-assistant/youker-assistant

« back to all changes in this revision

Viewing changes to mainui/main.cpp

  • Committer: lixiang
  • Date: 2018-03-06 03:13:06 UTC
  • Revision ID: lixiang@kylinos.cn-20180306031306-fd7qnru3vm4a1xjd
Rewrite with Qt5, and add system monitor

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2013 ~ 2015 National University of Defense Technology(NUDT) & Kylin Ltd.
3
 
 *
4
 
 * Authors:
5
 
 *  Kobe Lee    xiangli@ubuntukylin.com/kobe24_lixiang@126.com
6
 
 *
7
 
 * This program is free software; you can redistribute it and/or modify
8
 
 * it under the terms of the GNU General Public License as published by
9
 
 * the Free Software Foundation; version 3.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful,
12
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 * GNU General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 
 */
19
 
 
20
 
#include "mainwindow.h"
21
 
#include <QApplication>
22
 
#include <QTextCodec>
23
 
#include <QTranslator>
24
 
#include <QtSingleApplication>
25
 
#include <QScreen>
26
 
#include <QDebug>
27
 
#include <QPixmap>
28
 
#include <QSplashScreen>
29
 
#include <QElapsedTimer>
30
 
 
31
 
#include <unistd.h>
32
 
#include <fcntl.h>
33
 
#include <errno.h>
34
 
#include <pwd.h>
35
 
 
36
 
#define BUFF_SIZE (512)
37
 
char filePath[BUFF_SIZE] = {0};
38
 
 
39
 
#define LOCKFILE "/tmp/youker-assistant-%d.pid"
40
 
#define LOCKMODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
41
 
 
42
 
int make_pid_file() {
43
 
    char buf[16];
44
 
    struct flock fl;
45
 
    snprintf(filePath, BUFF_SIZE, LOCKFILE, getuid());
46
 
 
47
 
    int fd = open(filePath, O_RDWR|O_CREAT, LOCKMODE);
48
 
    if (fd < 0) {
49
 
        printf("Can not open %s: %s.\n", filePath, strerror(errno));
50
 
        return -1;
51
 
    }
52
 
 
53
 
    fl.l_type = F_WRLCK;
54
 
    fl.l_start = 0;
55
 
    fl.l_whence = SEEK_SET;
56
 
    fl.l_len = 0;
57
 
 
58
 
    if (fcntl(fd, F_SETLK, &fl) < 0) {
59
 
        printf("Can not lock %s: %s.\n", filePath, strerror(errno));
60
 
        close(fd);
61
 
        return -1;
62
 
    }
63
 
 
64
 
    ftruncate(fd, 0);
65
 
    sprintf(buf, "%d\n", getpid());
66
 
    write(fd, buf, strlen(buf));
67
 
    close(fd);
68
 
    return 0;
69
 
}
70
 
 
71
 
// Helper function to return display orientation as a string.
72
 
/*QString Orientation(Qt::ScreenOrientation orientation)
73
 
{
74
 
    switch (orientation) {
75
 
        case Qt::PrimaryOrientation           : return "Primary";
76
 
        case Qt::LandscapeOrientation         : return "Landscape";
77
 
        case Qt::PortraitOrientation          : return "Portrait";
78
 
        case Qt::InvertedLandscapeOrientation : return "Inverted landscape";
79
 
        case Qt::InvertedPortraitOrientation  : return "Inverted portrait";
80
 
        default                               : return "Unknown";
81
 
    }
82
 
}*/
83
 
 
84
 
/*int main(int argc, char *argv)
85
 
{
86
 
    QGuiApplication a(argc, &argv);
87
 
 
88
 
    qDebug() << "Number of screens:" << QGuiApplication::screens().size();
89
 
 
90
 
    qDebug() << "Primary screen:" << QGuiApplication::primaryScreen()->name();
91
 
 
92
 
    foreach (QScreen *screen, QGuiApplication::screens()) {
93
 
        qDebug() << "Information for screen:" << screen->name();
94
 
        qDebug() << "  Available geometry:" << screen->availableGeometry().x() << screen->availableGeometry().y() << screen->availableGeometry().width() << "x" << screen->availableGeometry().height();
95
 
        qDebug() << "  Available size:" << screen->availableSize().width() << "x" << screen->availableSize().height();
96
 
        qDebug() << "  Available virtual geometry:" << screen->availableVirtualGeometry().x() << screen->availableVirtualGeometry().y() << screen->availableVirtualGeometry().width() << "x" << screen->availableVirtualGeometry().height();
97
 
        qDebug() << "  Available virtual size:" << screen->availableVirtualSize().width() << "x" << screen->availableVirtualSize().height();
98
 
        qDebug() << "  Depth:" << screen->depth() << "bits";
99
 
        qDebug() << "  Geometry:" << screen->geometry().x() << screen->geometry().y() << screen->geometry().width() << "x" << screen->geometry().height();
100
 
        qDebug() << "  Logical DPI:" << screen->logicalDotsPerInch();
101
 
        qDebug() << "  Logical DPI X:" << screen->logicalDotsPerInchX();
102
 
        qDebug() << "  Logical DPI Y:" << screen->logicalDotsPerInchY();
103
 
        qDebug() << "  Orientation:" << Orientation(screen->orientation());
104
 
        qDebug() << "  Physical DPI:" << screen->physicalDotsPerInch();
105
 
        qDebug() << "  Physical DPI X:" << screen->physicalDotsPerInchX();
106
 
        qDebug() << "  Physical DPI Y:" << screen->physicalDotsPerInchY();
107
 
        qDebug() << "  Physical size:" << screen->physicalSize().width() << "x" << screen->physicalSize().height() << "mm";
108
 
        qDebug() << "  Primary orientation:" << Orientation(screen->primaryOrientation());
109
 
        qDebug() << "  Refresh rate:" << screen->refreshRate() << "Hz";
110
 
        qDebug() << "  Size:" << screen->size().width() << "x" << screen->size().height();
111
 
        qDebug() << "  Virtual geometry:" << screen->virtualGeometry().x() << screen->virtualGeometry().y() << screen->virtualGeometry().width() << "x" << screen->virtualGeometry().height();
112
 
        qDebug() << "  Virtual size:" << screen->virtualSize().width() << "x" << screen->virtualSize().height();
113
 
    }
114
 
}*/
115
 
 
116
 
 
117
 
 
118
 
 
119
 
 
120
 
int main(int argc, char *argv[])
121
 
{
122
 
//    QApplication app(argc, argv);
123
 
    //单程序运行处理
124
 
    QtSingleApplication app(argc, argv);
125
 
    if (app.isRunning())
126
 
    {
127
 
        qDebug() << QObject::tr("kylin-assistant had already running!");
128
 
        return 0;
129
 
    }
130
 
 
131
 
    QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
132
 
    QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());
133
 
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
134
 
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
135
 
 
136
 
    if (make_pid_file()) {
137
 
        exit(1);
138
 
    }
139
 
 
140
 
    QString arch = "";
141
 
 
142
 
#ifdef __x86_64__
143
 
        arch = "x86_64";
144
 
#elif __i386__
145
 
        arch = "i386";
146
 
#elif __aarch64__
147
 
        arch = "aarch64";
148
 
#endif
149
 
 
150
 
    QString locale = QLocale::system().name();
151
 
    QTranslator translator;
152
 
    if(locale == "zh_CN" || locale == "es" || locale == "fr" || locale == "de" || locale == "ru") {//中文 西班牙语 法语 德语 俄语
153
 
        if(!translator.load("youker-assistant_" + locale + ".qm",
154
 
                            ":/qmfile/translation/"))
155
 
            qDebug() << "Load translation file:"<< "youker-assistant_" + locale + ".qm" << " failed!";
156
 
        else
157
 
            app.installTranslator(&translator);
158
 
    }
159
 
 
160
 
    //加载Qt对话框默认的国际化
161
 
    QTranslator qtTranslator;
162
 
    qtTranslator.load("qt_" + locale,
163
 
                      QLibraryInfo::location(QLibraryInfo::TranslationsPath));
164
 
    app.installTranslator(&qtTranslator);
165
 
 
166
 
    QFile qss(":/qssfile/res/qss/youker-assistant.qss");
167
 
    qss.open(QFile::ReadOnly);
168
 
    qApp->setStyleSheet(qss.readAll());
169
 
    qss.close();
170
 
 
171
 
    QDesktopWidget *desktop = QApplication::desktop();
172
 
//    qDebug() << desktop->primaryScreen();//获取主屏幕的索引序号
173
 
    int sCount = desktop->screenCount();//获取当前显示器的个数
174
 
//    qDebug() << sCount;
175
 
    //如果有两个显示,则N=2,qt默认的计算机主机的index = 0,外接显示器的index = 1;
176
 
//    mdlg.setGeometry(desktop->screenGeometry(1));//QDialog 有个成员函数叫setGeometry,只需要将dialog对象的Geometry设置为index为1的显示器即可,默认为0.如果要显示的dialog的对象为mdlg,则
177
 
//    mdlg.show();
178
 
 
179
 
    //启动图片
180
 
    QPixmap pixmap("://res/skin/x.png");
181
 
    QSplashScreen screen(pixmap);
182
 
    screen.show();
183
 
    screen.showMessage("START...", Qt::AlignCenter, Qt::white);
184
 
 
185
 
    MainWindow w(arch, sCount);
186
 
 
187
 
#if 0
188
 
    //延时
189
 
    int delayTime = 3;
190
 
    QElapsedTimer timer;
191
 
    timer.start();
192
 
    while (timer.elapsed() < (delayTime * 1000)) {
193
 
        app.processEvents();
194
 
    }
195
 
    screen.finish(&w);
196
 
#endif
197
 
 
198
 
    w.setTranslator(&translator);
199
 
 
200
 
 
201
 
 
202
 
//    if (sCount > 1) {
203
 
//        w.setGeometry(desktop->screenGeometry(1));
204
 
//    }
205
 
//    w.display(sCount);
206
 
 
207
 
    return app.exec();
208
 
}
209
 
 
210
 
 
211
 
/*
212
 
测试多屏(只测了横屏,没测试纵屏): 根据当前屏幕数量n,生成n个窗口,每个窗口都占据了一个屏幕
213
 
#include "mainwindow.h"
214
 
 
215
 
#include <QApplication>
216
 
 
217
 
#include <QDesktopWidget>
218
 
 
219
 
#include <cstdio>
220
 
 
221
 
#include <QMessageBox>
222
 
 
223
 
 
224
 
 
225
 
typedef struct{
226
 
 
227
 
int screen_no;
228
 
 
229
 
QRect rect;
230
 
 
231
 
}SCREEN;
232
 
 
233
 
SCREEN g_screens[10];
234
 
 
235
 
 
236
 
 
237
 
int main(int argc, char *argv[])
238
 
 
239
 
{
240
 
 
241
 
QApplication app(argc, argv);
242
 
 
243
 
QDesktopWidget *desktop = QApplication::desktop();
244
 
 
245
 
 
246
 
int screen_count = desktop->screenCount();
247
 
 
248
 
 
249
 
int prim_screen = desktop->primaryScreen();
250
 
 
251
 
char warning[100], *idx=warning;
252
 
 
253
 
for(int i=0; i<screen_count ;i++ ){
254
 
 
255
 
g_screens[i].screen_no = prim_screen + i;
256
 
 
257
 
g_screens[i].rect = desktop->screenGeometry(prim_screen + i);
258
 
 
259
 
sprintf(idx, "screen%d w[%d], h[%d] ",i, g_screens[i].rect.width(),g_screens[i].rect.height() );
260
 
 
261
 
idx += strlen(idx);
262
 
 
263
 
}
264
 
 
265
 
sprintf(idx, "total width[%d] , total height[%d] \n", desktop->width(), desktop->height() );
266
 
 
267
 
QMessageBox::warning(NULL, "screen", warning, QMessageBox::Ok);
268
 
 
269
 
 
270
 
MainWindow wnd[5];
271
 
 
272
 
for(int i=0; i < screen_count; i++){
273
 
 
274
 
wnd[i].resize(g_screens[i].rect.width(),g_screens[i].rect.height());
275
 
 
276
 
if(i == 0)
277
 
 
278
 
wnd[i].move(0,0);
279
 
 
280
 
else
281
 
 
282
 
wnd[i].move(i* g_screens[i-1].rect.width(),0);
283
 
 
284
 
char str[50];
285
 
 
286
 
sprintf(str,"this is screen %d",i);
287
 
 
288
 
wnd[i].show();
289
 
 
290
 
}
291
 
 
292
 
return app.exec();
293
 
 
294
 
}
295
 
 * */