summaryrefslogtreecommitdiffstats
path: root/src/countdowndialog.cpp
blob: a3d9ac0f04915454698f5f22285337d9f6ee8a1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/***************************************************************************
 *   Copyright (C) 2007 by Danny Kukawka                                   *
 *                         <dkukawka@suse.de>, <danny.kukawka@web.de>      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of version 2 of the GNU General Public License     *
 *   as published by the Free Software Foundation.                         *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

/*! \file countdowndialog.cpp
 * \brief 	In this file can be found the countdown dialog related code. 
 * \author 	Danny Kukawka, <dkukawka@suse.de, danny.kukawka@web.de>
 * \date    	2007
 */

// own header
#include "countdowndialog.h"
#include "kpowersave_debug.h"

// KDE headers:
#include <tdelocale.h>
#include <kiconloader.h>
#include <kprogress.h>

// QT headers:
#include <tqdialog.h>
#include <tqlabel.h> 
#include <tqprogressbar.h>
#include <tqpushbutton.h>
#include <tqstring.h>
#include <tqtimer.h>

/*! This is the default constructor of class countDownDialog . */
countDownDialog::countDownDialog( int timeout, TQWidget *parent, const char *name)
		:countdown_Dialog(parent, name, false, TQt::WStyle_StaysOnTop | TQt::WDestructiveClose )
{
	kdDebugFuncIn(trace);
	chancel = false;
	remaining = timeout;
	timeOut = timeout;

	PROGRESS = new TQTimer(this);
	connect(PROGRESS, TQT_SIGNAL(timeout()), this, TQT_SLOT(updateProgress()));

	this->setCaption(i18n("KPowersave"));

	kdDebugFuncOut(trace);
}

/*! This is the default destructor of class countDownDialog . */
countDownDialog::~countDownDialog() {
	kdDebugFuncIn(trace);
	// no need to delete child widgets, TQt does it all for us
	emit dialogClosed(chancel);

	kdDebugFuncOut(trace);
}

/*! 
 * This used to set Icon/pixmap for the dialog.
 * \param type TQString with the type of the current suspend
 *             to set the pixmap in the dialog
 */
void countDownDialog::setPixmap( TQString type )
{
	TQPixmap pixmap = 0;

	if(type.startsWith("suspend2disk")){
		pixmap = TDEGlobal::iconLoader()->loadIcon("suspend_to_disk", TDEIcon::NoGroup, TDEIcon::SizeLarge);
	} else if (type.startsWith("suspend2ram")) {
		pixmap = TDEGlobal::iconLoader()->loadIcon("suspend_to_ram", TDEIcon::NoGroup, TDEIcon::SizeLarge);
	} else if (type.startsWith("standby")) {
		pixmap = TDEGlobal::iconLoader()->loadIcon("stand_by", TDEIcon::NoGroup, TDEIcon::SizeLarge);
	} else {
		pixmap = TDEGlobal::iconLoader()->loadIcon("kpowersave", TDEIcon::NoGroup, TDEIcon::SizeLarge);
	}
	iconPixmap->setPixmap( pixmap );
}

/*!
 * To set the message to the dialog, which should be shown to the user.
 * \param text	TQString with the message. 
 */
void countDownDialog::setMessageText(TQString text) {
	kdDebugFuncIn(trace);

	if (!text.isEmpty()) {
		textLabel->setText(text);
	}

	kdDebugFuncOut(trace);
}

/*!
 * To show the dialog and start the countdown.
 * \return boolean with the result of the operation
 * \retval true		if the dialog could get displayed
 * \retval false	if there is any problem
 */
bool countDownDialog::showDialog() {
	kdDebugFuncIn(trace);

	bool _retval = false;

	if (!textLabel->text().isEmpty() && timeOut > 0) {
		// init the progressbar
		progressBar->setFormat(i18n("%1 seconds").arg(remaining));
		progressBar->setPercentageVisible(true);
		progressBar->setProgress(100);
		progressBar->setEnabled(true);

		this->adjustSize();
		this->show();

		PROGRESS->start(1000, true);
	}

	kdDebugFuncOut(trace);
	return _retval;
}

/*!
 * \b TQT_SLOT to get the event if the 'Cancel' button was pressed.
 */
void countDownDialog::pB_cancel_pressed() {
	kdDebugFuncIn(trace);

	if (PROGRESS->isActive())
		PROGRESS->stop();

	chancel = true;
	close();

	kdDebugFuncOut(trace);
}

/*!
 * \b TQT_SLOT to handle the change of the progressbar.
 */
void countDownDialog::updateProgress() {
	kdDebugFuncIn(trace);

	if (remaining == 0 ) {
		if (PROGRESS->isActive())
			PROGRESS->stop();
		
		chancel = false;
		close();
	} else if ( remaining > 0) {
		int setTo = (int)((100.0/(float)timeOut)*(float)remaining);
		
		// set the progressBar
		progressBar->setFormat(i18n("%1 seconds").arg(remaining));
		progressBar->setPercentageVisible(true);
		progressBar->setProgress(setTo);
		progressBar->setEnabled(true);

		// increase counter
		remaining--;
		// start needed timer
		PROGRESS->start(1000, true);
	}

	kdDebugFuncOut(trace);
}

#include "countdowndialog.moc"