blob: d238a86d21dae5d46c2ca4bab0ca3eadd982c303 [file] [log] [blame] [raw]
/* PC GO Screen Shooter
* Copyright 2007-2014 PC GO Ld.
* Copyright 2015-2018 Rivoreo
*
* 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.
*/
/* Forked from Qt Demos. Original copyright notice and license:
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
** the names of its contributors may be used to endorse or promote
** products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
*/
#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 <QtGui/QClipboard>
#include <QtCore/QRect>
#include <QtCore/QDebug>
#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, copy_screenshot_button);
setTabOrder(copy_screenshot_button, 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());
}
}
void ScreenShot::CopyScreenShot() {
QApplication::clipboard()->setPixmap(originalPixmap);
}
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"), this, SLOT(NewScreenShot()));
saveScreenshotButton = CreateButton(tr("&Save"), this, SLOT(SaveScreenShot()));
quitScreenshotButton = CreateButton(tr("&Quit"), this, SLOT(close()));
copy_screenshot_button = CreateButton(tr("&Copy"), this, SLOT(CopyScreenShot()));
//spacer2 = new QSpacerItem(20, 20, QSizePolicy::Expanding);
spacer2 = new QSpacerItem(2, 2, QSizePolicy::Expanding);
buttonsLayout = new QHBoxLayout;
//buttonsLayout->addStretch();
buttonsLayout->addWidget(newScreenshotButton);
buttonsLayout->addWidget(quitScreenshotButton);
buttonsLayout->addItem(spacer2);
buttonsLayout->addWidget(copy_screenshot_button);
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);
//QSize size_hint = button->sizeHint();
QSize size_hint = button->minimumSizeHint();
if(size_hint.width() > 64) button->setMinimumWidth(64);
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]