2
* Copyright (C) 2013 ~ 2015 National University of Defense Technology(NUDT) & Kylin Ltd.
5
* Kobe Lee xiangli@ubuntukylin.com/kobe24_lixiang@126.com
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.
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.
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/>.
20
#include "mainwindow.h"
21
#include <QApplication>
23
#include <QTranslator>
24
#include <QtSingleApplication>
28
#include <QSplashScreen>
29
#include <QElapsedTimer>
36
#define BUFF_SIZE (512)
37
char filePath[BUFF_SIZE] = {0};
39
#define LOCKFILE "/tmp/youker-assistant-%d.pid"
40
#define LOCKMODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
45
snprintf(filePath, BUFF_SIZE, LOCKFILE, getuid());
47
int fd = open(filePath, O_RDWR|O_CREAT, LOCKMODE);
49
printf("Can not open %s: %s.\n", filePath, strerror(errno));
55
fl.l_whence = SEEK_SET;
58
if (fcntl(fd, F_SETLK, &fl) < 0) {
59
printf("Can not lock %s: %s.\n", filePath, strerror(errno));
65
sprintf(buf, "%d\n", getpid());
66
write(fd, buf, strlen(buf));
71
// Helper function to return display orientation as a string.
72
/*QString Orientation(Qt::ScreenOrientation orientation)
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";
84
/*int main(int argc, char *argv)
86
QGuiApplication a(argc, &argv);
88
qDebug() << "Number of screens:" << QGuiApplication::screens().size();
90
qDebug() << "Primary screen:" << QGuiApplication::primaryScreen()->name();
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();
120
int main(int argc, char *argv[])
122
// QApplication app(argc, argv);
124
QtSingleApplication app(argc, argv);
127
qDebug() << QObject::tr("kylin-assistant had already running!");
131
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
132
QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());
133
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
134
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
136
if (make_pid_file()) {
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!";
157
app.installTranslator(&translator);
161
QTranslator qtTranslator;
162
qtTranslator.load("qt_" + locale,
163
QLibraryInfo::location(QLibraryInfo::TranslationsPath));
164
app.installTranslator(&qtTranslator);
166
QFile qss(":/qssfile/res/qss/youker-assistant.qss");
167
qss.open(QFile::ReadOnly);
168
qApp->setStyleSheet(qss.readAll());
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,则
180
QPixmap pixmap("://res/skin/x.png");
181
QSplashScreen screen(pixmap);
183
screen.showMessage("START...", Qt::AlignCenter, Qt::white);
185
MainWindow w(arch, sCount);
192
while (timer.elapsed() < (delayTime * 1000)) {
198
w.setTranslator(&translator);
203
// w.setGeometry(desktop->screenGeometry(1));
205
// w.display(sCount);
212
测试多屏(只测了横屏,没测试纵屏): 根据当前屏幕数量n,生成n个窗口,每个窗口都占据了一个屏幕
213
#include "mainwindow.h"
215
#include <QApplication>
217
#include <QDesktopWidget>
221
#include <QMessageBox>
233
SCREEN g_screens[10];
237
int main(int argc, char *argv[])
241
QApplication app(argc, argv);
243
QDesktopWidget *desktop = QApplication::desktop();
246
int screen_count = desktop->screenCount();
249
int prim_screen = desktop->primaryScreen();
251
char warning[100], *idx=warning;
253
for(int i=0; i<screen_count ;i++ ){
255
g_screens[i].screen_no = prim_screen + i;
257
g_screens[i].rect = desktop->screenGeometry(prim_screen + i);
259
sprintf(idx, "screen%d w[%d], h[%d] ",i, g_screens[i].rect.width(),g_screens[i].rect.height() );
265
sprintf(idx, "total width[%d] , total height[%d] \n", desktop->width(), desktop->height() );
267
QMessageBox::warning(NULL, "screen", warning, QMessageBox::Ok);
272
for(int i=0; i < screen_count; i++){
274
wnd[i].resize(g_screens[i].rect.width(),g_screens[i].rect.height());
282
wnd[i].move(i* g_screens[i-1].rect.width(),0);
286
sprintf(str,"this is screen %d",i);