OSDN Git Service

844629b97867e697c4851c02af59db338a4cc58f
[qt-creator-jp/qt-creator-jp.git] / doc / pluginhowto / examples / loggermode / loggermodewidget.cpp
1 /***************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the documentation of Qt Creator.
8 **
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
21 **     the names of its contributors may be used to endorse or promote
22 **     products derived from this software without specific prior written
23 **     permission.
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ****************************************************************************/
38
39 #include "loggermodewidget.h"
40 #include<QTableWidget>
41 #include <QFileDialog>
42 #include<QLabel>
43 #include<QGroupBox>
44 #include<QComboBox>
45 #include<QPushButton>
46 #include<QLineEdit>
47 #include<QTime>
48 #include<QTimer>
49 #include<QTextEdit>
50 #include<QCalendarWidget>
51 #include<QComboBox>
52 #include<QGridLayout>
53 #include<QMessageBox>
54 #include<QApplication>
55 #include<QTextStream>
56 #include<QCloseEvent>
57
58 struct LoggerModeWidgetData
59 {
60     QLabel *progressLabel;
61     QLabel *hoursWorkedLabel;
62     QLabel *dateLabel;
63     QLabel *descriptionLabel;
64     QCalendarWidget *calendar;
65     QComboBox *progressComboBox;
66     QLineEdit *hoursWorkedLineEdit;
67     QPushButton *startTimerButton;
68     QPushButton *stopTimerButton;
69     QPushButton *saveButton;
70     QTimer *timer;
71     QTextEdit *textEdit;
72     QString projectName;
73     int totalTime;
74 };
75
76 LoggerModeWidget::LoggerModeWidget(const QString projectName, QWidget* parent)
77     :QWidget(parent)
78 {
79     d = new LoggerModeWidgetData;
80     d->projectName = projectName;
81     d->totalTime = 0;
82     /*
83         // Catch hold of the plugin-manager
84         ExtensionSystem::PluginManager* pm = ExtensionSystem::PluginManager::instance();
85
86         // Look for the ProjectExplorerPlugin object
87         ProjectExplorer::ProjectExplorerPlugin* projectExplorerPlugin
88         = pm->getObject<ProjectExplorer::ProjectExplorerPlugin>();
89
90         // Fetch a list of all open projects
91         QList<ProjectExplorer::Project*> projects =projectExplorerPlugin->session()->projects();
92         Q_FOREACH(ProjectExplorer::Project* project, projects)
93              d->projectExplorerCombo->addItem( project->name());
94     */
95     QStringList percentList;
96     percentList <<"10%" <<"20%" <<"30%" <<"40%" <<"50%"
97                 <<"60%" <<"70%" <<"80%" <<"90%" <<"100%" ;
98     d->progressLabel = new QLabel("Progress:");
99     d->hoursWorkedLabel = new QLabel("Hours Worked:");
100     d->dateLabel = new QLabel("Date:");
101     d->descriptionLabel = new QLabel("Description :");
102     d->hoursWorkedLineEdit = new QLineEdit;
103     d->hoursWorkedLineEdit->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
104     d->progressComboBox = new QComboBox;
105     d->progressComboBox->addItems(percentList);
106     d->progressComboBox->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
107     d->startTimerButton = new QPushButton(tr("Start Timer"));
108     d->startTimerButton->setFixedWidth(80);
109     d->stopTimerButton = new QPushButton(tr("Pause Timer"));
110     d->stopTimerButton->setFixedWidth(80);
111     d->stopTimerButton->setCheckable(true);
112     d->textEdit = new QTextEdit(this);
113     d->textEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
114     d->calendar = new QCalendarWidget;
115     d->saveButton = new QPushButton(tr("Save To File"));
116     d->saveButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
117
118     QGroupBox *timeLoggerBox = new QGroupBox(tr("Time Logger"));
119
120     QGridLayout *gLayout = new QGridLayout;
121     gLayout->addWidget(d->dateLabel, 0, 0, 1, 1);
122     gLayout->addWidget(d->calendar, 1, 0, 1, 3);
123     gLayout->addWidget(d->progressLabel, 2, 0, 1, 1);
124     gLayout->addWidget(d->progressComboBox, 2, 1, 1, 1);
125     gLayout->addWidget(d->hoursWorkedLabel, 3, 0, 1, 1);
126     gLayout->addWidget(d->hoursWorkedLineEdit, 3, 1, 1, 1);
127     gLayout->addWidget(d->startTimerButton, 4, 1, 1, 1);
128     gLayout->addWidget(d->stopTimerButton, 4, 2, 1, 1);
129     timeLoggerBox->setLayout(gLayout);
130
131     d->timer = new QTimer(this);
132    //d->time.setHMS(0,0,0);
133
134     // connection of SIGNALS and SLOTS
135
136     connect(d->timer, SIGNAL(timeout()), this, SLOT(updateTime()));
137     connect(d->startTimerButton,SIGNAL(clicked()),this,SLOT(startTimeLog()));
138     connect(d->stopTimerButton,SIGNAL(clicked()),this,SLOT(endTimeLog()));
139     connect(d->saveButton, SIGNAL(clicked()), this, SLOT(saveToFile()));
140
141
142     QVBoxLayout *vLayout = new QVBoxLayout;
143     vLayout->addWidget(d->descriptionLabel);
144     vLayout->addWidget(d->textEdit);
145
146     QHBoxLayout * hLayout = new QHBoxLayout;
147     hLayout->addWidget(timeLoggerBox);
148     hLayout->addLayout(vLayout);
149
150     QHBoxLayout *bLayout = new QHBoxLayout;
151     bLayout->addStretch(1);
152     bLayout->addWidget(d->saveButton);
153
154     QVBoxLayout *mainLayout = new QVBoxLayout(this);
155     mainLayout->addLayout(hLayout);
156     mainLayout->addLayout(bLayout);
157     mainLayout->addStretch(1);
158
159 }
160
161 LoggerModeWidget::~LoggerModeWidget()
162 {
163     delete d;
164 }
165
166 bool LoggerModeWidget::saveToFile()
167 {
168     QString fileName = QFileDialog::getSaveFileName(this);
169     if (fileName.isEmpty())
170         return false;
171
172     QFile file(fileName);
173     if (!file.open(QFile::WriteOnly | QFile::Text)) {
174         QMessageBox::critical(this, tr("Application"),
175                              tr("Unable to open file %1 for writing :\n%2.")
176                              .arg(fileName)
177                              .arg(file.errorString()));
178         return false;
179     }
180
181     QTextStream out(&file);
182
183 #ifndef QT_NO_CURSOR
184     QApplication::setOverrideCursor(Qt::WaitCursor);
185 #endif
186     out << "Project name : " << d->projectName << "\n";
187     out << "Date         : " << d->calendar->selectedDate().toString() << "\n";
188     out << "Progress     : " << d->progressComboBox->currentText() << "\n";
189     out << "Duration     : " << d->hoursWorkedLineEdit->text() << "\n\n";
190     out << "Description  : " << d->textEdit->toPlainText();
191 #ifndef QT_NO_CURSOR
192     QApplication::restoreOverrideCursor();
193 #endif
194
195     return true;
196 }
197
198 void LoggerModeWidget::startTimeLog()
199 {
200     d->totalTime = 0;
201     d->timer->start(1000);
202 }
203
204 void LoggerModeWidget::endTimeLog()
205 {
206     if(d->stopTimerButton->isChecked())
207     {
208         d->stopTimerButton->setText("Continue Timer");
209         d->timer->stop();
210     }
211     else
212     {
213         d->stopTimerButton->setText("Pause Timer");
214         d->timer->start(1000);
215     }
216 }
217
218 void LoggerModeWidget::updateTime()
219 {
220     d->totalTime++;
221     QTime time(0,0,0);
222     time = time.addSecs(d->totalTime);
223     d->hoursWorkedLineEdit->setText(time.toString());
224 }
225 /*
226 void LoggerModeWidget::setProjectName(QString name)
227 {
228     d->projectName = name;
229 }
230 */