ctkScreenshotDialog.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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()), SLOT(onCaptureButtonClicked()));
  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. q->setWidgetToGrab(0);
  56. }
  57. //-----------------------------------------------------------------------------
  58. void ctkScreenshotDialogPrivate::setWaitingForScreenshot(bool waiting)
  59. {
  60. this->DelaySpinBox->setDisabled(waiting);
  61. this->ButtonBox->setDisabled(waiting);
  62. }
  63. //-----------------------------------------------------------------------------
  64. void ctkScreenshotDialogPrivate::onCaptureButtonClicked()
  65. {
  66. Q_Q(ctkScreenshotDialog);
  67. q->saveScreenshot(this->DelaySpinBox->value());
  68. }
  69. //-----------------------------------------------------------------------------
  70. void ctkScreenshotDialogPrivate::updateFullNameLabel()
  71. {
  72. QString text("%1_%2.png");
  73. this->ImageFullNameLabel->setText(
  74. text.arg(this->ImageNameLineEdit->text()).arg(this->ImageVersionNumberSpinBox->value()));
  75. }
  76. //-----------------------------------------------------------------------------
  77. void ctkScreenshotDialogPrivate::setCountDownLabel(int newValue)
  78. {
  79. this->CountDownLabel->setText(QString("%1").arg(newValue));
  80. }
  81. //-----------------------------------------------------------------------------
  82. void ctkScreenshotDialogPrivate::resetCountDownValue()
  83. {
  84. this->CountDownTimer.stop();
  85. this->CountDownValue = this->DelaySpinBox->value();
  86. this->setCountDownLabel(this->CountDownValue);
  87. }
  88. //-----------------------------------------------------------------------------
  89. void ctkScreenshotDialogPrivate::updateCountDown()
  90. {
  91. this->CountDownValue--;
  92. this->setCountDownLabel(this->CountDownValue);
  93. }
  94. //-----------------------------------------------------------------------------
  95. // ctkScreenshotDialog methods
  96. //-----------------------------------------------------------------------------
  97. ctkScreenshotDialog::ctkScreenshotDialog(QWidget* newParent)
  98. : Superclass(newParent)
  99. , d_ptr(new ctkScreenshotDialogPrivate(*this))
  100. {
  101. Q_D(ctkScreenshotDialog);
  102. d->init();
  103. }
  104. //-----------------------------------------------------------------------------
  105. ctkScreenshotDialog::~ctkScreenshotDialog()
  106. {
  107. }
  108. //-----------------------------------------------------------------------------
  109. void ctkScreenshotDialog::setWidgetToGrab(QWidget* newWidgetToGrab)
  110. {
  111. Q_D(ctkScreenshotDialog);
  112. d->OptionGroupBox->setEnabled(newWidgetToGrab != 0);
  113. d->CaptureButton->setEnabled(newWidgetToGrab != 0);
  114. d->WidgetToGrab = newWidgetToGrab;
  115. }
  116. //-----------------------------------------------------------------------------
  117. QWidget* ctkScreenshotDialog::widgetToGrab() const
  118. {
  119. Q_D(const ctkScreenshotDialog);
  120. return d->WidgetToGrab.data();
  121. }
  122. //-----------------------------------------------------------------------------
  123. void ctkScreenshotDialog::setImageName(const QString& newImageName)
  124. {
  125. Q_D(ctkScreenshotDialog);
  126. d->ImageNameLineEdit->setText(newImageName);
  127. }
  128. //-----------------------------------------------------------------------------
  129. QString ctkScreenshotDialog::imageName() const
  130. {
  131. Q_D(const ctkScreenshotDialog);
  132. return d->ImageNameLineEdit->text();
  133. }
  134. //-----------------------------------------------------------------------------
  135. void ctkScreenshotDialog::setImageDirectory(const QString& newDirectory)
  136. {
  137. Q_D(ctkScreenshotDialog);
  138. d->DirectoryButton->setDirectory(newDirectory);
  139. }
  140. //-----------------------------------------------------------------------------
  141. QString ctkScreenshotDialog::imageDirectory()const
  142. {
  143. Q_D(const ctkScreenshotDialog);
  144. return d->DirectoryButton->directory();
  145. }
  146. //-----------------------------------------------------------------------------
  147. void ctkScreenshotDialog::saveScreenshot(int delayInSeconds)
  148. {
  149. Q_D(ctkScreenshotDialog);
  150. if (d->WidgetToGrab.isNull())
  151. {
  152. return;
  153. }
  154. if (delayInSeconds > 0)
  155. {
  156. d->setWaitingForScreenshot(true);
  157. d->CountDownTimer.start(1000);
  158. QTimer::singleShot(delayInSeconds * 1000, this, SLOT(saveScreenshot()));
  159. return;
  160. }
  161. QPixmap viewportPixmap = QPixmap::grabWidget(d->WidgetToGrab.data());
  162. if (d->DelaySpinBox->value() != 0)
  163. {
  164. d->setWaitingForScreenshot(false);
  165. d->resetCountDownValue();
  166. qApp->beep();
  167. }
  168. // Rescale
  169. QPixmap rescaledViewportPixmap = viewportPixmap.scaled(
  170. viewportPixmap.size().width() * d->ScaleFactorSpinBox->value(),
  171. viewportPixmap.size().height() * d->ScaleFactorSpinBox->value());
  172. QString filename = QString("%1/%2_%3.png").arg(d->DirectoryButton->directory())
  173. .arg(d->ImageNameLineEdit->text())
  174. .arg(d->ImageVersionNumberSpinBox->value());
  175. // Check if file exists
  176. bool overwrite = d->OverwriteCheckBox->isChecked();
  177. if (QFile::exists(filename) && !overwrite)
  178. {
  179. int answer = QMessageBox::question(this, "Screen Capture",
  180. tr("File already exists. Overwrite ?"),
  181. QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No);
  182. if (answer == QMessageBox::YesToAll)
  183. {
  184. overwrite = true;
  185. d->OverwriteCheckBox->setChecked(true);
  186. }
  187. else if(answer == QMessageBox::Yes)
  188. {
  189. overwrite = true;
  190. }
  191. else
  192. {
  193. return;
  194. }
  195. }
  196. rescaledViewportPixmap.save(filename);
  197. d->ImageVersionNumberSpinBox->setValue(d->ImageVersionNumberSpinBox->value() + 1);
  198. }