blob: a32c321f75229a8fecaab3e444031fb693c9ceef [file] [log] [blame] [raw]
/* PC GO Screen Shooter
Copyright 2007-2014 PC GO Ld.
Original are from Qt Demos
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
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.
*/
#include "screenshot.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <QtCore/QDir>
#include <QtCore/QDateTime>
#include <QtCore/QTimer>
#include <QtCore/QSettings>
#include <QtGui/QApplication>
#include <QtGui/QWidget>
#include <QtGui/QDesktopWidget>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QCheckBox>
#include <QtGui/QPushButton>
#include <QtGui/QGroupBox>
#include <QtGui/QSpinBox>
#include <QtGui/QVBoxLayout>
#include <QtGui/QMessageBox>
#include <QtCore/QRect>
#define CONFIG_FILE_NAME "screen-shooter.cfg"
//#define DATE_FORMAT "yyyy-M-d.H-mm-ss.png"
#define DATE_FORMAT "yyyy-MM-dd.HH-mm-ss"
//! [0]
ScreenShot::ScreenShot() {
screenshotLabel = new QLabel;
screenshotLabel->setSizePolicy(QSizePolicy::Expanding,
QSizePolicy::Expanding);
screenshotLabel->setAlignment(Qt::AlignCenter);
screenshotLabel->setMinimumSize(240, 160);
InitOptions();
CreateButtonsLayout();
mainLayout = new QVBoxLayout;
mainLayout->addWidget(screenshotLabel);
mainLayout->addWidget(optionsGroupBox);
mainLayout->addLayout(buttonsLayout);
setLayout(mainLayout);
ShootScreen();
delaySpinBox->setValue(config->value("Delay", 5).toInt());
setWindowTitle(tr("PC GO Screen Shooter"));
resize(240, 200);
connect(filename_edit_box, SIGNAL(returnPressed()), saveScreenshotButton, SLOT(click()));
connect(path_edit_box, SIGNAL(returnPressed()), saveScreenshotButton, SLOT(click()));
setTabOrder(saveScreenshotButton, filename_edit_box);
setTabOrder(filename_edit_box, name_as_time);
setTabOrder(name_as_time, path_edit_box);
setTabOrder(path_edit_box, delaySpinBox);
setTabOrder(delaySpinBox, hideThisWindowCheckBox);
setTabOrder(hideThisWindowCheckBox, quitScreenshotButton);
setTabOrder(quitScreenshotButton, newScreenshotButton);
//this->setFocus();
//move(center());
}
//! [0]
//! [1]
void ScreenShot::resizeEvent(QResizeEvent * /* event */)
{
QSize scaledSize = originalPixmap.size();
scaledSize.scale(screenshotLabel->size(), Qt::KeepAspectRatio);
if (!screenshotLabel->pixmap()
|| scaledSize != screenshotLabel->pixmap()->size())
UpdateScreenShotLabel();
}
//! [1]
//! [2]
void ScreenShot::NewScreenShot() {
if (hideThisWindowCheckBox->isChecked()) hide();
newScreenshotButton->setDisabled(true);
QTimer::singleShot(delaySpinBox->value() * 1000, this, SLOT(ShootScreen()));
config->setValue("Delay", delaySpinBox->value());
config->setValue("HideWindow", hideThisWindowCheckBox->isChecked());
}
//! [2]
//! [3]
void ScreenShot::SaveScreenShot() {
if(!name_as_time->isChecked()) config->setValue("SaveFileName", filename_edit_box->text());
config->setValue("NameAsTime", name_as_time->isChecked());
config->setValue("SavePath", path_edit_box->text());
QString filename = filename_edit_box->text();
QString fullfilename = path_edit_box->text() + '/' + filename;
int i = filename.lastIndexOf('.');
if(i == -1) {
QMessageBox::critical(this, tr("Error"), tr("I don't know format of the image"));
return;
}
QString format = filename.right(filename.length() - i - 1).toLower();
if(originalPixmap.save(fullfilename, format.toLocal8Bit())) QApplication::quit();
else QMessageBox::critical(this, tr("Error"), tr("Failed to save screenshot"));
}
//! [3]
//! [4]
void ScreenShot::ShootScreen() {
//if (delaySpinBox->value() != 0)
//qApp->beep();
if(delaySpinBox->value()) QApplication::beep();
//! [4]
originalPixmap = QPixmap(); // clear image for low memory situations
// on embedded devices.
//! [5]
originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
UpdateScreenShotLabel();
newScreenshotButton->setDisabled(false);
if(hideThisWindowCheckBox->isChecked()) show();
}
//! [5]
//! [6]
void ScreenShot::UpdateCheckBox_HideWindow() {
hideThisWindowCheckBox->setEnabled(delaySpinBox->value());
}
//! [6]
void ScreenShot::UpdateFilenameEdit(bool t) {
filename_edit_box->setEnabled(!t);
if(t) {
filename_edit_box->setText(GetSaveFileNameByTime());
} else {
filename_edit_box->setText(config->value("SaveFileName", "screenshot.png").toString());
}
}
QString ScreenShot::GetSaveFileNameByTime() {
QString timestr = QDateTime::currentDateTime().toString(DATE_FORMAT);
return "screenshot." + timestr + ".png";
}
QString ScreenShot::config_dir() {
QString appath = QApplication::applicationDirPath();
#ifndef Q_OS_WINCE
if(QFile::exists(appath + "/" CONFIG_FILE_NAME)) {
#endif
return appath;
#ifndef Q_OS_WINCE
}
QString inhome = QDir::homePath() + "/.pc-go";
if(!QFile::exists(inhome)) mkdir(inhome.toLocal8Bit().data(), 0755);
return inhome;
#endif
}
QPoint ScreenShot::center() {
return QPoint((QApplication::desktop()->width() - width()) / 2, (QApplication::desktop()->height() - height()) / 2);
}
//! [7]
void ScreenShot::InitOptions() {
config = new QSettings(config_dir() + "/" CONFIG_FILE_NAME, (QSettings::Format)1, this);
config->setIniCodec("UTF-8");
optionsGroupBox = new QGroupBox(this);//(tr("Options"), this);
LabelsLayout = new QVBoxLayout;
FilenameLayout = new QHBoxLayout;
PathLayout = new QHBoxLayout;
filename_label = new QLabel(tr("Filename:"), optionsGroupBox);
filename_edit_box = new QLineEdit(optionsGroupBox);
name_as_time = new QCheckBox("Name as &Time", optionsGroupBox);
name_as_time->setChecked(config->value("NameAsTime", true).toBool());
if(name_as_time->isChecked()) {
filename_edit_box->setEnabled(false);
filename_edit_box->setText(GetSaveFileNameByTime());
} else {
filename_edit_box->setText(config->value("SaveFileName", "screenshot.png").toString());
}
connect(name_as_time, SIGNAL(toggled(bool)), this, SLOT(UpdateFilenameEdit(bool)));
path_label = new QLabel(tr("Save to:"), optionsGroupBox);
path_edit_box = new QLineEdit(config->value("SavePath", QApplication::applicationDirPath()).toString(), this);
line1 = new QFrame(optionsGroupBox);
line1->setFrameShape(QFrame::HLine);
line1->setFrameShadow(QFrame::Sunken);
line2 = new QFrame(optionsGroupBox);
line2->setFrameShape(QFrame::HLine);
//line2->setFrameShadow(QFrame::Sunken);
delaySpinBox = new QSpinBox;
delaySpinBox->setSuffix(tr(" s"));
delaySpinBox->setMaximum(60);
connect(delaySpinBox, SIGNAL(valueChanged(int)), this, SLOT(UpdateCheckBox_HideWindow()));
delaySpinBoxLabel = new QLabel(tr("Delay:"));
spacer1 = new QSpacerItem(235, 20, QSizePolicy::Expanding);
hideThisWindowCheckBox = new QCheckBox(tr("&Hide This Window"));
hideThisWindowCheckBox->setChecked(config->value("HideWindow", false).toBool());
LabelsLayout->addWidget(filename_label);
LabelsLayout->addWidget(path_label);
LabelsLayout->addWidget(line2);
LabelsLayout->addWidget(delaySpinBoxLabel);
FilenameLayout->addWidget(filename_edit_box);
FilenameLayout->addWidget(name_as_time);
PathLayout->addWidget(path_edit_box);
optionsGroupBoxLayout = new QGridLayout;
optionsGroupBoxLayout->addLayout(LabelsLayout, 0, 0, 4, 1);
optionsGroupBoxLayout->addLayout(FilenameLayout, 0, 1, 1, 3);
optionsGroupBoxLayout->addLayout(PathLayout, 1, 1, 1, 3);
//optionsGroupBoxLayout->addWidget(line1, 2, 0, 1, 4);
optionsGroupBoxLayout->addWidget(line1, 2, 1, 1, 3);
optionsGroupBoxLayout->addWidget(delaySpinBox, 3, 1);
optionsGroupBoxLayout->addItem(spacer1, 3, 2);
optionsGroupBoxLayout->addWidget(hideThisWindowCheckBox, 3, 3);
optionsGroupBox->setLayout(optionsGroupBoxLayout);
}
//! [7]
//! [8]
void ScreenShot::CreateButtonsLayout() {
newScreenshotButton = CreateButton(tr("&New Screenshot"), this, SLOT(NewScreenShot()));
saveScreenshotButton = CreateButton(tr("&Save"), this, SLOT(SaveScreenShot()));
quitScreenshotButton = CreateButton(tr("&Quit"), this, SLOT(close()));
spacer2 = new QSpacerItem(400, 20, QSizePolicy::Expanding);
buttonsLayout = new QHBoxLayout;
//buttonsLayout->addStretch();
buttonsLayout->addWidget(newScreenshotButton);
buttonsLayout->addItem(spacer2);
buttonsLayout->addWidget(quitScreenshotButton);
buttonsLayout->addWidget(saveScreenshotButton);
saveScreenshotButton->setAutoDefault(true);
saveScreenshotButton->setDefault(true);
saveScreenshotButton->setFocus();
//saveScreenshotButton->setFocusPolicy(Qt::WheelFocus);
//saveScreenshotButton->setShortcut(Qt::Key_Return);
quitScreenshotButton->setShortcut(Qt::Key_Escape);
}
//! [8]
//! [9]
QPushButton *ScreenShot::CreateButton(const QString &text, QWidget *receiver,
const char *member)
{
QPushButton *button = new QPushButton(text);
button->connect(button, SIGNAL(clicked()), receiver, member);
return button;
}
//! [9]
//! [10]
void ScreenShot::UpdateScreenShotLabel()
{
screenshotLabel->setPixmap(originalPixmap.scaled(screenshotLabel->size(),
Qt::KeepAspectRatio,
Qt::SmoothTransformation));
}
//! [10]