ctkScreenshotDialog.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QDebug>
  16. #include <QPushButton>
  17. #include <QMessageBox>
  18. // CTK includes
  19. #include "ctkScreenshotDialog.h"
  20. #include "ctkScreenshotDialog_p.h"
  21. //-----------------------------------------------------------------------------
  22. // ctkScreenshotDialogPrivate methods
  23. //-----------------------------------------------------------------------------
  24. ctkScreenshotDialogPrivate::ctkScreenshotDialogPrivate(ctkScreenshotDialog& object)
  25. : QObject(&object), q_ptr(&object)
  26. {
  27. this->CaptureButton = 0;
  28. this->CountDownValue = 0;
  29. this->AspectRatio = 1.0;
  30. }
  31. //-----------------------------------------------------------------------------
  32. ctkScreenshotDialogPrivate::~ctkScreenshotDialogPrivate()
  33. {
  34. }
  35. //-----------------------------------------------------------------------------
  36. void ctkScreenshotDialogPrivate::init()
  37. {
  38. Q_Q(ctkScreenshotDialog);
  39. this->setupUi(q);
  40. }
  41. //-----------------------------------------------------------------------------
  42. void ctkScreenshotDialogPrivate::setupUi(QDialog * widget)
  43. {
  44. Q_Q(ctkScreenshotDialog);
  45. this->Ui_ctkScreenshotDialog::setupUi(widget);
  46. QPushButton * okButton = this->ButtonBox->button(QDialogButtonBox::Ok);
  47. Q_ASSERT(okButton);
  48. // Update OK button text
  49. okButton->setText("Capture");
  50. connect(okButton, SIGNAL(clicked()), q, SLOT(saveScreenshot()));
  51. connect(this->ImageNameLineEdit, SIGNAL(textChanged(QString)), SLOT(updateFullNameLabel()));
  52. connect(this->ImageVersionNumberSpinBox, SIGNAL(valueChanged(int)), SLOT(updateFullNameLabel()));
  53. connect(this->DelaySpinBox, SIGNAL(valueChanged(int)), SLOT(resetCountDownValue()));
  54. connect(&this->CountDownTimer, SIGNAL(timeout()), SLOT(updateCountDown()));
  55. connect(this->ScaleFactorRadioButton, SIGNAL(toggled(bool)), SLOT(selectScaleFactor(bool)));
  56. connect(this->OutputResolutionRadioButton, SIGNAL(toggled(bool)), SLOT(selectOutputResolution(bool)));
  57. connect(this->LockAspectToolButton, SIGNAL(toggled(bool)), SLOT(lockAspectRatio(bool)));
  58. connect(this->WidthLineEdit, SIGNAL(editingFinished()), SLOT(onWidthEdited()));
  59. connect(this->HeightLineEdit, SIGNAL(editingFinished()), SLOT(onHeightEdited()));
  60. this->CaptureButton = okButton;
  61. // Called to enable/disable buttons
  62. q->setWidgetToGrab(0);
  63. }
  64. //-----------------------------------------------------------------------------
  65. void ctkScreenshotDialogPrivate::setWaitingForScreenshot(bool waiting)
  66. {
  67. this->DelaySpinBox->setDisabled(waiting);
  68. this->ButtonBox->setDisabled(waiting);
  69. }
  70. //-----------------------------------------------------------------------------
  71. bool ctkScreenshotDialogPrivate::isWaitingForScreenshot()const
  72. {
  73. Q_Q(const ctkScreenshotDialog);
  74. // Bad Qt const correctness, need to hack.
  75. ctkScreenshotDialog* parent = const_cast<ctkScreenshotDialog*>(q);
  76. Q_ASSERT(this->DelaySpinBox->isEnabledTo(parent) ==
  77. this->ButtonBox->isEnabledTo(parent));
  78. return this->ButtonBox->isEnabledTo(parent);
  79. }
  80. //-----------------------------------------------------------------------------
  81. void ctkScreenshotDialogPrivate::updateFullNameLabel()
  82. {
  83. QString text("%1_%2.png");
  84. this->ImageFullNameLabel->setText(
  85. text.arg(this->ImageNameLineEdit->text())
  86. .arg(this->ImageVersionNumberSpinBox->value()));
  87. }
  88. //-----------------------------------------------------------------------------
  89. void ctkScreenshotDialogPrivate::setCountDownLabel(int newValue)
  90. {
  91. this->CountDownLabel->setText(QString("%1").arg(newValue));
  92. }
  93. //-----------------------------------------------------------------------------
  94. void ctkScreenshotDialogPrivate::resetCountDownValue()
  95. {
  96. this->CountDownTimer.stop();
  97. this->CountDownValue = this->DelaySpinBox->value();
  98. this->setCountDownLabel(this->CountDownValue);
  99. }
  100. //-----------------------------------------------------------------------------
  101. void ctkScreenshotDialogPrivate::updateCountDown()
  102. {
  103. this->setCountDownLabel(--this->CountDownValue);
  104. }
  105. //-----------------------------------------------------------------------------
  106. void ctkScreenshotDialogPrivate::useScalarFactor(bool scale)
  107. {
  108. this->ScaleFactorSpinBox->setEnabled(scale);
  109. this->WidthLineEdit->setEnabled(!scale);
  110. this->HeightLineEdit->setEnabled(!scale);
  111. this->xLabel->setEnabled(!scale);
  112. this->LockAspectToolButton->setEnabled(!scale);
  113. }
  114. //-----------------------------------------------------------------------------
  115. void ctkScreenshotDialogPrivate::selectScaleFactor(bool scale)
  116. {
  117. this->useScalarFactor(scale);
  118. }
  119. //-----------------------------------------------------------------------------
  120. void ctkScreenshotDialogPrivate::selectOutputResolution(bool scale)
  121. {
  122. this->useScalarFactor(!scale);
  123. }
  124. //-----------------------------------------------------------------------------
  125. void ctkScreenshotDialogPrivate::lockAspectRatio(bool lock)
  126. {
  127. if(lock)
  128. {
  129. QPixmap viewportPixmap = QPixmap::grabWidget(this->WidgetToGrab.data());
  130. QSize curSize = viewportPixmap.size();
  131. this->AspectRatio = curSize.width()/static_cast<double>(curSize.height());
  132. }
  133. }
  134. //-----------------------------------------------------------------------------
  135. void ctkScreenshotDialogPrivate::onWidthEdited()
  136. {
  137. if(this->LockAspectToolButton->isChecked())
  138. {
  139. this->HeightLineEdit->setText(QString::number(static_cast<int>(this->WidthLineEdit->text().toInt()/this->AspectRatio)));
  140. }
  141. }
  142. //-----------------------------------------------------------------------------
  143. void ctkScreenshotDialogPrivate::onHeightEdited()
  144. {
  145. if(this->LockAspectToolButton->isChecked())
  146. {
  147. this->WidthLineEdit->setText(QString::number(static_cast<int>(this->HeightLineEdit->text().toInt()*this->AspectRatio)));
  148. }
  149. }
  150. //-----------------------------------------------------------------------------
  151. void ctkScreenshotDialog::enforceResolution(int width, int height)
  152. {
  153. Q_D(ctkScreenshotDialog);
  154. d->OutputResolutionRadioButton->setChecked(true);
  155. d->useScalarFactor(true);
  156. d->ScaleFactorRadioButton->setEnabled(false);
  157. d->OutputResolutionRadioButton->setEnabled(false);
  158. d->ScaleFactorSpinBox->setEnabled(false);
  159. d->WidthLineEdit->setText(QString::number(width));
  160. d->HeightLineEdit->setText(QString::number(height));
  161. }
  162. //-----------------------------------------------------------------------------
  163. void ctkScreenshotDialogPrivate::saveScreenshot(int delayInSeconds)
  164. {
  165. Q_Q(ctkScreenshotDialog);
  166. if (this->WidgetToGrab.isNull())
  167. {
  168. return;
  169. }
  170. if (delayInSeconds <= 0)
  171. {
  172. q->instantScreenshot();
  173. return;
  174. }
  175. this->setWaitingForScreenshot(true);
  176. this->CountDownValue = delayInSeconds;
  177. this->CountDownTimer.start(1000);
  178. // Add 1ms to give time to set the countdown at 0.
  179. QTimer::singleShot(delayInSeconds * 1000 + 1, q, SLOT(instantScreenshot()));
  180. }
  181. //-----------------------------------------------------------------------------
  182. // ctkScreenshotDialog methods
  183. //-----------------------------------------------------------------------------
  184. ctkScreenshotDialog::ctkScreenshotDialog(QWidget* newParent)
  185. : Superclass(newParent)
  186. , d_ptr(new ctkScreenshotDialogPrivate(*this))
  187. {
  188. Q_D(ctkScreenshotDialog);
  189. d->init();
  190. }
  191. //-----------------------------------------------------------------------------
  192. ctkScreenshotDialog::~ctkScreenshotDialog()
  193. {
  194. }
  195. //-----------------------------------------------------------------------------
  196. void ctkScreenshotDialog::setWidgetToGrab(QWidget* newWidgetToGrab)
  197. {
  198. Q_D(ctkScreenshotDialog);
  199. d->OptionGroupBox->setEnabled(newWidgetToGrab != 0);
  200. d->CaptureButton->setEnabled(newWidgetToGrab != 0);
  201. d->WidgetToGrab = newWidgetToGrab;
  202. }
  203. //-----------------------------------------------------------------------------
  204. QWidget* ctkScreenshotDialog::widgetToGrab() const
  205. {
  206. Q_D(const ctkScreenshotDialog);
  207. return d->WidgetToGrab.data();
  208. }
  209. //-----------------------------------------------------------------------------
  210. void ctkScreenshotDialog::setBaseFileName(const QString& newBaseName)
  211. {
  212. Q_D(ctkScreenshotDialog);
  213. d->ImageNameLineEdit->setText(newBaseName);
  214. }
  215. //-----------------------------------------------------------------------------
  216. QString ctkScreenshotDialog::baseFileName() const
  217. {
  218. Q_D(const ctkScreenshotDialog);
  219. return d->ImageNameLineEdit->text();
  220. }
  221. //-----------------------------------------------------------------------------
  222. void ctkScreenshotDialog::setDirectory(const QString& newDirectory)
  223. {
  224. Q_D(ctkScreenshotDialog);
  225. d->DirectoryButton->setDirectory(newDirectory);
  226. }
  227. //-----------------------------------------------------------------------------
  228. QString ctkScreenshotDialog::directory()const
  229. {
  230. Q_D(const ctkScreenshotDialog);
  231. return d->DirectoryButton->directory();
  232. }
  233. //-----------------------------------------------------------------------------
  234. void ctkScreenshotDialog::setDelay(int seconds)
  235. {
  236. Q_D(ctkScreenshotDialog);
  237. d->DelaySpinBox->setValue(seconds);
  238. }
  239. //-----------------------------------------------------------------------------
  240. int ctkScreenshotDialog::delay()const
  241. {
  242. Q_D(const ctkScreenshotDialog);
  243. return d->DelaySpinBox->value();
  244. }
  245. //-----------------------------------------------------------------------------
  246. void ctkScreenshotDialog::saveScreenshot()
  247. {
  248. Q_D(ctkScreenshotDialog);
  249. d->saveScreenshot(this->delay());
  250. }
  251. //-----------------------------------------------------------------------------
  252. void ctkScreenshotDialog::instantScreenshot()
  253. {
  254. Q_D(ctkScreenshotDialog);
  255. if (d->WidgetToGrab.isNull())
  256. {
  257. return;
  258. }
  259. QPixmap viewportPixmap = QPixmap::grabWidget(d->WidgetToGrab.data());
  260. if (d->isWaitingForScreenshot() && d->DelaySpinBox->value() != 0)
  261. {
  262. qApp->beep();
  263. }
  264. d->setWaitingForScreenshot(false);
  265. d->resetCountDownValue();
  266. // Rescale based on scale factor or output resolution specified
  267. QPixmap rescaledViewportPixmap = viewportPixmap;
  268. if(d->ScaleFactorRadioButton->isChecked())
  269. {
  270. rescaledViewportPixmap = viewportPixmap.scaled(
  271. viewportPixmap.size().width() * d->ScaleFactorSpinBox->value(),
  272. viewportPixmap.size().height() * d->ScaleFactorSpinBox->value());
  273. }
  274. else if(d->OutputResolutionRadioButton->isChecked())
  275. {
  276. rescaledViewportPixmap = viewportPixmap.scaled(
  277. d->WidthLineEdit->text().toInt(),
  278. d->HeightLineEdit->text().toInt());
  279. }
  280. QString filename = QString("%1/%2_%3.png").arg(d->DirectoryButton->directory())
  281. .arg(d->ImageNameLineEdit->text())
  282. .arg(d->ImageVersionNumberSpinBox->value());
  283. // Check if file exists
  284. bool overwrite = d->OverwriteCheckBox->isChecked();
  285. if (QFile::exists(filename) && !overwrite)
  286. {
  287. int answer = QMessageBox::question(this, "Screen Capture",
  288. tr("File already exists. Overwrite ?"),
  289. QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No);
  290. if (answer == QMessageBox::YesToAll)
  291. {
  292. overwrite = true;
  293. d->OverwriteCheckBox->setChecked(true);
  294. }
  295. else if(answer == QMessageBox::Yes)
  296. {
  297. overwrite = true;
  298. }
  299. else
  300. {
  301. return;
  302. }
  303. }
  304. rescaledViewportPixmap.save(filename);
  305. d->ImageVersionNumberSpinBox->setValue(d->ImageVersionNumberSpinBox->value() + 1);
  306. }