ctkScreenshotDialog.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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.commontk.org/LICENSE
  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. }
  30. //-----------------------------------------------------------------------------
  31. ctkScreenshotDialogPrivate::~ctkScreenshotDialogPrivate()
  32. {
  33. }
  34. //-----------------------------------------------------------------------------
  35. void ctkScreenshotDialogPrivate::init()
  36. {
  37. Q_Q(ctkScreenshotDialog);
  38. this->setupUi(q);
  39. }
  40. //-----------------------------------------------------------------------------
  41. void ctkScreenshotDialogPrivate::setupUi(QDialog * widget)
  42. {
  43. Q_Q(ctkScreenshotDialog);
  44. this->Ui_ctkScreenshotDialog::setupUi(widget);
  45. QPushButton * okButton = this->ButtonBox->button(QDialogButtonBox::Ok);
  46. Q_ASSERT(okButton);
  47. // Update OK button text
  48. okButton->setText("Capture");
  49. connect(okButton, SIGNAL(clicked()), q, SLOT(saveScreenshot()));
  50. connect(this->ImageNameLineEdit, SIGNAL(textChanged(QString)), SLOT(updateFullNameLabel()));
  51. connect(this->ImageVersionNumberSpinBox, SIGNAL(valueChanged(int)), SLOT(updateFullNameLabel()));
  52. connect(this->DelaySpinBox, SIGNAL(valueChanged(int)), SLOT(resetCountDownValue()));
  53. connect(&this->CountDownTimer, SIGNAL(timeout()), SLOT(updateCountDown()));
  54. this->CaptureButton = okButton;
  55. // Called to enable/disable buttons
  56. q->setWidgetToGrab(0);
  57. }
  58. //-----------------------------------------------------------------------------
  59. void ctkScreenshotDialogPrivate::setWaitingForScreenshot(bool waiting)
  60. {
  61. this->DelaySpinBox->setDisabled(waiting);
  62. this->ButtonBox->setDisabled(waiting);
  63. }
  64. //-----------------------------------------------------------------------------
  65. bool ctkScreenshotDialogPrivate::isWaitingForScreenshot()const
  66. {
  67. Q_Q(const ctkScreenshotDialog);
  68. // Bad Qt const correctness, need to hack.
  69. ctkScreenshotDialog* parent = const_cast<ctkScreenshotDialog*>(q);
  70. Q_ASSERT(this->DelaySpinBox->isEnabledTo(parent) ==
  71. this->ButtonBox->isEnabledTo(parent));
  72. return this->ButtonBox->isEnabledTo(parent);
  73. }
  74. //-----------------------------------------------------------------------------
  75. void ctkScreenshotDialogPrivate::updateFullNameLabel()
  76. {
  77. QString text("%1_%2.png");
  78. this->ImageFullNameLabel->setText(
  79. text.arg(this->ImageNameLineEdit->text())
  80. .arg(this->ImageVersionNumberSpinBox->value()));
  81. }
  82. //-----------------------------------------------------------------------------
  83. void ctkScreenshotDialogPrivate::setCountDownLabel(int newValue)
  84. {
  85. this->CountDownLabel->setText(QString("%1").arg(newValue));
  86. }
  87. //-----------------------------------------------------------------------------
  88. void ctkScreenshotDialogPrivate::resetCountDownValue()
  89. {
  90. this->CountDownTimer.stop();
  91. this->CountDownValue = this->DelaySpinBox->value();
  92. this->setCountDownLabel(this->CountDownValue);
  93. }
  94. //-----------------------------------------------------------------------------
  95. void ctkScreenshotDialogPrivate::updateCountDown()
  96. {
  97. this->setCountDownLabel(--this->CountDownValue);
  98. }
  99. //-----------------------------------------------------------------------------
  100. void ctkScreenshotDialogPrivate::saveScreenshot(int delayInSeconds)
  101. {
  102. Q_Q(ctkScreenshotDialog);
  103. if (this->WidgetToGrab.isNull())
  104. {
  105. return;
  106. }
  107. if (delayInSeconds <= 0)
  108. {
  109. q->instantScreenshot();
  110. return;
  111. }
  112. this->setWaitingForScreenshot(true);
  113. this->CountDownValue = delayInSeconds;
  114. this->CountDownTimer.start(1000);
  115. // Add 1ms to give time to set the countdown at 0.
  116. QTimer::singleShot(delayInSeconds * 1000 + 1, q, SLOT(instantScreenshot()));
  117. }
  118. //-----------------------------------------------------------------------------
  119. // ctkScreenshotDialog methods
  120. //-----------------------------------------------------------------------------
  121. ctkScreenshotDialog::ctkScreenshotDialog(QWidget* newParent)
  122. : Superclass(newParent)
  123. , d_ptr(new ctkScreenshotDialogPrivate(*this))
  124. {
  125. Q_D(ctkScreenshotDialog);
  126. d->init();
  127. }
  128. //-----------------------------------------------------------------------------
  129. ctkScreenshotDialog::~ctkScreenshotDialog()
  130. {
  131. }
  132. //-----------------------------------------------------------------------------
  133. void ctkScreenshotDialog::setWidgetToGrab(QWidget* newWidgetToGrab)
  134. {
  135. Q_D(ctkScreenshotDialog);
  136. d->OptionGroupBox->setEnabled(newWidgetToGrab != 0);
  137. d->CaptureButton->setEnabled(newWidgetToGrab != 0);
  138. d->WidgetToGrab = newWidgetToGrab;
  139. }
  140. //-----------------------------------------------------------------------------
  141. QWidget* ctkScreenshotDialog::widgetToGrab() const
  142. {
  143. Q_D(const ctkScreenshotDialog);
  144. return d->WidgetToGrab.data();
  145. }
  146. //-----------------------------------------------------------------------------
  147. void ctkScreenshotDialog::setBaseFileName(const QString& newBaseName)
  148. {
  149. Q_D(ctkScreenshotDialog);
  150. d->ImageNameLineEdit->setText(newBaseName);
  151. }
  152. //-----------------------------------------------------------------------------
  153. QString ctkScreenshotDialog::baseFileName() const
  154. {
  155. Q_D(const ctkScreenshotDialog);
  156. return d->ImageNameLineEdit->text();
  157. }
  158. //-----------------------------------------------------------------------------
  159. void ctkScreenshotDialog::setDirectory(const QString& newDirectory)
  160. {
  161. Q_D(ctkScreenshotDialog);
  162. d->DirectoryButton->setDirectory(newDirectory);
  163. }
  164. //-----------------------------------------------------------------------------
  165. QString ctkScreenshotDialog::directory()const
  166. {
  167. Q_D(const ctkScreenshotDialog);
  168. return d->DirectoryButton->directory();
  169. }
  170. //-----------------------------------------------------------------------------
  171. void ctkScreenshotDialog::setDelay(int seconds)
  172. {
  173. Q_D(ctkScreenshotDialog);
  174. d->DelaySpinBox->setValue(seconds);
  175. }
  176. //-----------------------------------------------------------------------------
  177. int ctkScreenshotDialog::delay()const
  178. {
  179. Q_D(const ctkScreenshotDialog);
  180. return d->DelaySpinBox->value();
  181. }
  182. //-----------------------------------------------------------------------------
  183. void ctkScreenshotDialog::saveScreenshot()
  184. {
  185. Q_D(ctkScreenshotDialog);
  186. d->saveScreenshot(this->delay());
  187. }
  188. //-----------------------------------------------------------------------------
  189. void ctkScreenshotDialog::instantScreenshot()
  190. {
  191. Q_D(ctkScreenshotDialog);
  192. if (d->WidgetToGrab.isNull())
  193. {
  194. return;
  195. }
  196. QPixmap viewportPixmap = QPixmap::grabWidget(d->WidgetToGrab.data());
  197. if (d->isWaitingForScreenshot() && d->DelaySpinBox->value() != 0)
  198. {
  199. qApp->beep();
  200. }
  201. d->setWaitingForScreenshot(false);
  202. d->resetCountDownValue();
  203. // Rescale
  204. QPixmap rescaledViewportPixmap = viewportPixmap.scaled(
  205. viewportPixmap.size().width() * d->ScaleFactorSpinBox->value(),
  206. viewportPixmap.size().height() * d->ScaleFactorSpinBox->value());
  207. QString filename = QString("%1/%2_%3.png").arg(d->DirectoryButton->directory())
  208. .arg(d->ImageNameLineEdit->text())
  209. .arg(d->ImageVersionNumberSpinBox->value());
  210. // Check if file exists
  211. bool overwrite = d->OverwriteCheckBox->isChecked();
  212. if (QFile::exists(filename) && !overwrite)
  213. {
  214. int answer = QMessageBox::question(this, "Screen Capture",
  215. tr("File already exists. Overwrite ?"),
  216. QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No);
  217. if (answer == QMessageBox::YesToAll)
  218. {
  219. overwrite = true;
  220. d->OverwriteCheckBox->setChecked(true);
  221. }
  222. else if(answer == QMessageBox::Yes)
  223. {
  224. overwrite = true;
  225. }
  226. else
  227. {
  228. return;
  229. }
  230. }
  231. rescaledViewportPixmap.save(filename);
  232. d->ImageVersionNumberSpinBox->setValue(d->ImageVersionNumberSpinBox->value() + 1);
  233. }