Files
qt-creator/doc/pluginhowto/examples/loggermode/loggermodewidget.cpp

193 lines
5.9 KiB
C++

#include "loggermodewidget.h"
#include<QTableWidget>
#include <QFileDialog>
#include<QLabel>
#include<QGroupBox>
#include<QComboBox>
#include<QPushButton>
#include<QLineEdit>
#include<QTime>
#include<QTimer>
#include<QTextEdit>
#include<QCalendarWidget>
#include<QComboBox>
#include<QGridLayout>
#include<QMessageBox>
#include<QApplication>
#include<QTextStream>
#include<QCloseEvent>
struct LoggerModeWidgetData
{
QLabel *progressLabel;
QLabel *hoursWorkedLabel;
QLabel *dateLabel;
QLabel *descriptionLabel;
QCalendarWidget *calendar;
QComboBox *progressComboBox;
QLineEdit *hoursWorkedLineEdit;
QPushButton *startTimerButton;
QPushButton *stopTimerButton;
QPushButton *saveButton;
QTimer *timer;
QTextEdit *textEdit;
QString projectName;
int totalTime;
};
LoggerModeWidget::LoggerModeWidget(const QString projectName, QWidget* parent)
:QWidget(parent)
{
d = new LoggerModeWidgetData;
d->projectName = projectName;
d->totalTime = 0;
/*
// Catch hold of the plugin-manager
ExtensionSystem::PluginManager* pm = ExtensionSystem::PluginManager::instance();
// Look for the ProjectExplorerPlugin object
ProjectExplorer::ProjectExplorerPlugin* projectExplorerPlugin
= pm->getObject<ProjectExplorer::ProjectExplorerPlugin>();
// Fetch a list of all open projects
QList<ProjectExplorer::Project*> projects =projectExplorerPlugin->session()->projects();
Q_FOREACH(ProjectExplorer::Project* project, projects)
d->projectExplorerCombo->addItem( project->name());
*/
QStringList percentList;
percentList <<"10%" <<"20%" <<"30%" <<"40%" <<"50%"
<<"60%" <<"70%" <<"80%" <<"90%" <<"100%" ;
d->progressLabel = new QLabel("Progress:");
d->hoursWorkedLabel = new QLabel("Hours Worked:");
d->dateLabel = new QLabel("Date:");
d->descriptionLabel = new QLabel("Description :");
d->hoursWorkedLineEdit = new QLineEdit;
d->hoursWorkedLineEdit->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
d->progressComboBox = new QComboBox;
d->progressComboBox->addItems(percentList);
d->progressComboBox->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
d->startTimerButton = new QPushButton(tr("Start Timer"));
d->startTimerButton->setFixedWidth(80);
d->stopTimerButton = new QPushButton(tr("Pause Timer"));
d->stopTimerButton->setFixedWidth(80);
d->stopTimerButton->setCheckable(true);
d->textEdit = new QTextEdit(this);
d->textEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
d->calendar = new QCalendarWidget;
d->saveButton = new QPushButton(tr("Save To File"));
d->saveButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
QGroupBox *timeLoggerBox = new QGroupBox(tr("Time Logger"));
QGridLayout *gLayout = new QGridLayout;
gLayout->addWidget(d->dateLabel, 0, 0, 1, 1);
gLayout->addWidget(d->calendar, 1, 0, 1, 3);
gLayout->addWidget(d->progressLabel, 2, 0, 1, 1);
gLayout->addWidget(d->progressComboBox, 2, 1, 1, 1);
gLayout->addWidget(d->hoursWorkedLabel, 3, 0, 1, 1);
gLayout->addWidget(d->hoursWorkedLineEdit, 3, 1, 1, 1);
gLayout->addWidget(d->startTimerButton, 4, 1, 1, 1);
gLayout->addWidget(d->stopTimerButton, 4, 2, 1, 1);
timeLoggerBox->setLayout(gLayout);
d->timer = new QTimer(this);
//d->time.setHMS(0,0,0);
// connection of SIGNALS and SLOTS
connect(d->timer, SIGNAL(timeout()), this, SLOT(updateTime()));
connect(d->startTimerButton,SIGNAL(clicked()),this,SLOT(startTimeLog()));
connect(d->stopTimerButton,SIGNAL(clicked()),this,SLOT(endTimeLog()));
connect(d->saveButton, SIGNAL(clicked()), this, SLOT(saveToFile()));
QVBoxLayout *vLayout = new QVBoxLayout;
vLayout->addWidget(d->descriptionLabel);
vLayout->addWidget(d->textEdit);
QHBoxLayout * hLayout = new QHBoxLayout;
hLayout->addWidget(timeLoggerBox);
hLayout->addLayout(vLayout);
QHBoxLayout *bLayout = new QHBoxLayout;
bLayout->addStretch(1);
bLayout->addWidget(d->saveButton);
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addLayout(hLayout);
mainLayout->addLayout(bLayout);
mainLayout->addStretch(1);
}
LoggerModeWidget::~LoggerModeWidget()
{
delete d;
}
bool LoggerModeWidget::saveToFile()
{
QString fileName = QFileDialog::getSaveFileName(this);
if (fileName.isEmpty())
return false;
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::critical(this, tr("Application"),
tr("Unable to open file %1 for writing :\n%2.")
.arg(fileName)
.arg(file.errorString()));
return false;
}
QTextStream out(&file);
#ifndef QT_NO_CURSOR
QApplication::setOverrideCursor(Qt::WaitCursor);
#endif
out << "Project name : " << d->projectName << "\n";
out << "Date : " << d->calendar->selectedDate().toString() << "\n";
out << "Progress : " << d->progressComboBox->currentText() << "\n";
out << "Duration : " << d->hoursWorkedLineEdit->text() << "\n\n";
out << "Description : " << d->textEdit->toPlainText();
#ifndef QT_NO_CURSOR
QApplication::restoreOverrideCursor();
#endif
return true;
}
void LoggerModeWidget::startTimeLog()
{
d->totalTime = 0;
d->timer->start(1000);
}
void LoggerModeWidget::endTimeLog()
{
if(d->stopTimerButton->isChecked())
{
d->stopTimerButton->setText("Continue Timer");
d->timer->stop();
}
else
{
d->stopTimerButton->setText("Pause Timer");
d->timer->start(1000);
}
}
void LoggerModeWidget::updateTime()
{
d->totalTime++;
QTime time(0,0,0);
time = time.addSecs(d->totalTime);
d->hoursWorkedLineEdit->setText(time.toString());
}
/*
void LoggerModeWidget::setProjectName(QString name)
{
d->projectName = name;
}
*/