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

« back to all changes in this revision

Viewing changes to src/slidershow.cpp

  • Committer: kobe
  • Date: 2015-02-13 07:37:10 UTC
  • Revision ID: xiangli@ubuntukylin.com-20150213073710-0jyp02ilyi5njj10
Qt Version

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2013 ~ 2014 National University of Defense Technology(NUDT) & Kylin Ltd.
3
 
 * Author: Kobe Lee
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License as published by
7
 
 * the Free Software Foundation; version 3.
8
 
 *
9
 
 * This program is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 * GNU General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License
15
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 
 */
17
 
 
18
 
#include "slidershow.h"
19
 
#include <QDebug>
20
 
#include <QDesktopWidget>
21
 
//513 417    680 370
22
 
 
23
 
SliderShow::SliderShow(QWidget *parent)
24
 
    :QDialog(parent)
25
 
{
26
 
    current_index = 0;
27
 
    page_count = 5;
28
 
    window_btn_count = 5;
29
 
 
30
 
    this->setWindowFlags(Qt::ToolTip | Qt::FramelessWindowHint);
31
 
    this->setAttribute(Qt::WA_TranslucentBackground);
32
 
    this->resize(QSize(680, 372));
33
 
 
34
 
    //背景底片
35
 
    background_label = new QLabel(this);
36
 
    background_label->setPixmap(QPixmap(":/pixmap/image/bg_bottom"));
37
 
    background_label->setGeometry(QRect(0, 0, this->width(), this->height()));
38
 
 
39
 
    //把所有要显示的图片设置到一个大的label中
40
 
    QPixmap pixmap(QSize(this->width()*page_count, 372));
41
 
    QPainter painter(&pixmap);
42
 
    for(int i = 0; i < page_count; i++) {
43
 
        painter.drawImage(QRect(680*i, 0, 680, 372), QImage(QString(":/pixmap/image/desktop_%1").arg(i)));
44
 
    }
45
 
    master_label = new QLabel(this);
46
 
    master_label->resize(pixmap.size());
47
 
    master_label->setPixmap(pixmap);
48
 
    master_label->move(0, 0);
49
 
 
50
 
    //设置图片上的index按钮
51
 
    for(int i = 0; i < window_btn_count; i++)
52
 
    {
53
 
        IconText *label;
54
 
        label = new IconText(i, this);
55
 
        label->resize(QSize(14, 14));
56
 
        label->setIcon(QPixmap(QString(":/pixmap/image/dot_normal")));
57
 
        label->move(300+i*14, 319);
58
 
        connect(label, SIGNAL(ukclicked(int)), this, SLOT(changeCurrentPage(int)));
59
 
        pic_array[i] = label;
60
 
        label->raise();
61
 
    }
62
 
    pic_array[0]->setMousePressFlag(true);
63
 
    pic_array[0]->installEventFilter(this);
64
 
    pic_array[1]->installEventFilter(this);
65
 
    pic_array[2]->installEventFilter(this);
66
 
    pic_array[3]->installEventFilter(this);
67
 
    pic_array[4]->installEventFilter(this);
68
 
    //设置隐藏按钮属性
69
 
    close_button = new KButton(this);
70
 
    close_button->setPicName(":/pixmap/image/closeBtn");
71
 
    close_button->resize(QSize(26, 20));
72
 
    close_button->move(1, 1);
73
 
    close_button->raise();
74
 
    close_button->installEventFilter(this);
75
 
    connect(close_button, SIGNAL(clicked()), this, SLOT(accept()));
76
 
 
77
 
    //设置开始按钮属性
78
 
    hLayout = new QHBoxLayout(this);
79
 
    start_button = new KButton(this);
80
 
 
81
 
    locale_Lan = this->get_locale_version();
82
 
    if(locale_Lan == "zh_CN") {
83
 
        start_button->setPicName(":/pixmap/image/start");
84
 
    }
85
 
    else {
86
 
        start_button->setPicName(":/pixmap/image/start_en");
87
 
    }
88
 
    start_button->resize(QSize(180, 57));
89
 
    start_button->raise();
90
 
    start_button->hide();
91
 
    hLayout->addWidget(start_button);
92
 
    hLayout->setAlignment(Qt::AlignBottom | Qt::AlignHCenter);
93
 
    start_button->installEventFilter(this);
94
 
    connect(start_button, SIGNAL(clicked()), this, SLOT(start_run_program()));
95
 
 
96
 
    timer=new QTimer(this);
97
 
    connect(timer,SIGNAL(timeout()),this,SLOT(timerChangePosition()));
98
 
    timer->start(3000);
99
 
 
100
 
    int windowWidth = QApplication::desktop()->width();
101
 
    int windowHeight = QApplication::desktop()->height();
102
 
    this->move((windowWidth - this->width()) / 2,(windowHeight - this->height()) / 2);
103
 
}
104
 
 
105
 
SliderShow::~SliderShow()
106
 
{
107
 
    for (int i = 0; i < page_count; i++) {
108
 
        delete pic_array[i];
109
 
    }
110
 
    delete master_label;
111
 
    delete close_button;
112
 
    delete background_label;
113
 
    delete start_button;
114
 
 
115
 
    disconnect(timer,SIGNAL(timeout()),this,SLOT(timerChangePosition()));
116
 
    if(timer->isActive()) {
117
 
        timer->stop();
118
 
    }
119
 
    if (timer != NULL) {
120
 
        delete timer;
121
 
    }
122
 
}
123
 
 
124
 
QString SliderShow::get_locale_version() {
125
 
    QString locale = QLocale::system().name();
126
 
    return locale;
127
 
}
128
 
 
129
 
bool SliderShow::eventFilter(QObject *obj, QEvent *event) {
130
 
    if(obj == start_button || obj == close_button || obj == pic_array[0] || obj == pic_array[1] || obj == pic_array[2] || obj == pic_array[3] || obj == pic_array[4])
131
 
    {
132
 
        if (((QMouseEvent *)event)->button() == Qt::LeftButton) {
133
 
            if (event->type() == QEvent::MouseButtonPress) {
134
 
                mouse_move = false;
135
 
            }
136
 
        }
137
 
    }
138
 
    return QObject::eventFilter(obj, event);
139
 
}
140
 
 
141
 
void SliderShow::mousePressEvent(QMouseEvent *event) {
142
 
    if (event->button() == Qt::LeftButton) {
143
 
        dragPos = event->globalPos() - frameGeometry().topLeft();
144
 
        event->accept();
145
 
        mouse_move = true;
146
 
    }
147
 
}
148
 
 
149
 
void SliderShow::mouseMoveEvent(QMouseEvent *event) {
150
 
    if (event->buttons() & Qt::LeftButton ) {
151
 
        if(mouse_move) {
152
 
            move(event->globalPos() - dragPos);
153
 
            setWindowOpacity(0.7);
154
 
        }
155
 
    }
156
 
    event->accept();
157
 
}
158
 
 
159
 
void SliderShow::mouseReleaseEvent(QMouseEvent *event) {
160
 
    if (event->button() == Qt::LeftButton) {
161
 
        setWindowOpacity(1);
162
 
    }
163
 
    event->accept();
164
 
}
165
 
 
166
 
//点击按钮时更换成对应的图片
167
 
void SliderShow::changeCurrentPage(int index/*IconText *iconText*/)
168
 
{
169
 
    timer->stop();
170
 
    //更换按钮状态
171
 
    for(int i = 0; i < window_btn_count; i++)
172
 
    {
173
 
        if(index == i) {
174
 
            pic_array[i]->setMousePressFlag(true);
175
 
        }
176
 
        else {
177
 
            pic_array[i]->setMousePressFlag(false);
178
 
        }
179
 
    }
180
 
        if(index == 4) {
181
 
            start_button->show();
182
 
        }
183
 
        else {
184
 
            start_button->hide();
185
 
        }
186
 
 
187
 
    int current_pos_x = master_label->x();
188
 
    int dest_pos_x = -680 * index;
189
 
    if(current_pos_x > dest_pos_x) {
190
 
        while(current_pos_x > dest_pos_x)
191
 
        {
192
 
            master_label->move(current_pos_x-20, 0);
193
 
            current_pos_x = master_label->x();
194
 
            qApp->processEvents(QEventLoop::AllEvents);//防止界面冻结
195
 
        }
196
 
    }
197
 
    else if(current_pos_x < dest_pos_x) {
198
 
        while(current_pos_x < dest_pos_x)
199
 
        {
200
 
            master_label->move(current_pos_x+20, 0);
201
 
            current_pos_x = master_label->x();
202
 
            qApp->processEvents(QEventLoop::AllEvents);//防止界面冻结
203
 
        }
204
 
    }
205
 
    master_label->move(dest_pos_x, 0);
206
 
    current_index = index;
207
 
    timer->start();
208
 
}
209
 
 
210
 
void SliderShow::timerChangePosition() {
211
 
    if(current_index == page_count-1) {
212
 
        current_index = 0;
213
 
        pic_array[0]->setMousePressFlag(true);
214
 
        this->changeCurrentPage(current_index);
215
 
    }
216
 
    else {
217
 
        ++current_index;
218
 
        this->changeCurrentPage(current_index);
219
 
    }
220
 
}
221
 
 
222
 
void SliderShow::start_run_program() {
223
 
    this->accept();
224
 
}