ctkScreenshotDialog.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. this->AllowTransparency = true;
  31. }
  32. //-----------------------------------------------------------------------------
  33. ctkScreenshotDialogPrivate::~ctkScreenshotDialogPrivate()
  34. {
  35. }
  36. //-----------------------------------------------------------------------------
  37. void ctkScreenshotDialogPrivate::init()
  38. {
  39. Q_Q(ctkScreenshotDialog);
  40. this->setupUi(q);
  41. }
  42. //-----------------------------------------------------------------------------
  43. void ctkScreenshotDialogPrivate::setupUi(QDialog * widget)
  44. {
  45. Q_Q(ctkScreenshotDialog);
  46. this->Ui_ctkScreenshotDialog::setupUi(widget);
  47. QPushButton * okButton = this->ButtonBox->button(QDialogButtonBox::Ok);
  48. Q_ASSERT(okButton);
  49. // Update OK button text
  50. okButton->setText("Capture");
  51. connect(okButton, SIGNAL(clicked()), q, SLOT(saveScreenshot()));
  52. connect(this->ImageNameLineEdit, SIGNAL(textChanged(QString)), SLOT(updateFullNameLabel()));
  53. connect(this->ImageVersionNumberSpinBox, SIGNAL(valueChanged(int)), SLOT(updateFullNameLabel()));
  54. connect(this->DelaySpinBox, SIGNAL(valueChanged(int)), SLOT(resetCountDownValue()));
  55. connect(&this->CountDownTimer, SIGNAL(timeout()), SLOT(updateCountDown()));
  56. connect(this->ScaleFactorRadioButton, SIGNAL(toggled(bool)), SLOT(selectScaleFactor(bool)));
  57. connect(this->OutputResolutionRadioButton, SIGNAL(toggled(bool)), SLOT(selectOutputResolution(bool)));
  58. connect(this->LockAspectToolButton, SIGNAL(toggled(bool)), SLOT(lockAspectRatio(bool)));
  59. connect(this->WidthSpinBox, SIGNAL(editingFinished()), SLOT(onWidthEdited()));
  60. connect(this->HeightSpinBox, SIGNAL(editingFinished()), SLOT(onHeightEdited()));
  61. this->CaptureButton = okButton;
  62. // Called to enable/disable buttons
  63. q->setWidgetToGrab(0);
  64. // Set a sufficient range (1, 2^16) on the spin boxes
  65. this->WidthSpinBox->setRange(1, 65536);
  66. this->HeightSpinBox->setRange(1, 65536);
  67. this->DirectoryPathLineEdit->setFilters(ctkPathLineEdit::Dirs);
  68. }
  69. //-----------------------------------------------------------------------------
  70. void ctkScreenshotDialogPrivate::setWaitingForScreenshot(bool waiting)
  71. {
  72. this->DelaySpinBox->setDisabled(waiting);
  73. this->ButtonBox->setDisabled(waiting);
  74. }
  75. //-----------------------------------------------------------------------------
  76. bool ctkScreenshotDialogPrivate::isWaitingForScreenshot()const
  77. {
  78. Q_Q(const ctkScreenshotDialog);
  79. // Bad Qt const correctness, need to hack.
  80. ctkScreenshotDialog* parent = const_cast<ctkScreenshotDialog*>(q);
  81. Q_ASSERT(this->DelaySpinBox->isEnabledTo(parent) ==
  82. this->ButtonBox->isEnabledTo(parent));
  83. return this->ButtonBox->isEnabledTo(parent);
  84. }
  85. //-----------------------------------------------------------------------------
  86. void ctkScreenshotDialogPrivate::updateFullNameLabel()
  87. {
  88. QString text("%1_%2.png");
  89. this->ImageFullNameLabel->setText(
  90. text.arg(this->ImageNameLineEdit->text())
  91. .arg(this->ImageVersionNumberSpinBox->value()));
  92. }
  93. //-----------------------------------------------------------------------------
  94. void ctkScreenshotDialogPrivate::setCountDownLabel(int newValue)
  95. {
  96. this->CountDownLabel->setText(QString("%1").arg(newValue));
  97. }
  98. //-----------------------------------------------------------------------------
  99. void ctkScreenshotDialogPrivate::resetCountDownValue()
  100. {
  101. this->CountDownTimer.stop();
  102. this->CountDownValue = this->DelaySpinBox->value();
  103. this->setCountDownLabel(this->CountDownValue);
  104. }
  105. //-----------------------------------------------------------------------------
  106. void ctkScreenshotDialogPrivate::updateCountDown()
  107. {
  108. this->setCountDownLabel(--this->CountDownValue);
  109. }
  110. //-----------------------------------------------------------------------------
  111. void ctkScreenshotDialogPrivate::useScalarFactor(bool scale)
  112. {
  113. this->ScaleFactorSpinBox->setEnabled(scale);
  114. this->WidthSpinBox->setEnabled(!scale);
  115. this->HeightSpinBox->setEnabled(!scale);
  116. this->xLabel->setEnabled(!scale);
  117. this->LockAspectToolButton->setEnabled(!scale);
  118. }
  119. //-----------------------------------------------------------------------------
  120. void ctkScreenshotDialogPrivate::selectScaleFactor(bool scale)
  121. {
  122. this->useScalarFactor(scale);
  123. }
  124. //-----------------------------------------------------------------------------
  125. void ctkScreenshotDialogPrivate::selectOutputResolution(bool scale)
  126. {
  127. this->useScalarFactor(!scale);
  128. }
  129. //-----------------------------------------------------------------------------
  130. void ctkScreenshotDialogPrivate::lockAspectRatio(bool lock)
  131. {
  132. Q_Q(ctkScreenshotDialog);
  133. if(lock)
  134. {
  135. QSize curSize = q->widgetSize();
  136. if(curSize.height() > 0)
  137. {
  138. this->AspectRatio = curSize.width()/static_cast<double>(curSize.height());
  139. }
  140. else
  141. {
  142. QString message = QString("Height of widget: ") + curSize.height() +\
  143. QString(" is invalid. Check widget dimensions. Using default aspect\
  144. ratio (1.0).");
  145. QMessageBox::warning(q, "Invalid widget dimensions", message,
  146. QMessageBox::Ok);
  147. this->AspectRatio = 1.0;
  148. }
  149. }
  150. }
  151. //-----------------------------------------------------------------------------
  152. void ctkScreenshotDialogPrivate::onWidthEdited()
  153. {
  154. Q_Q(ctkScreenshotDialog);
  155. if(this->LockAspectToolButton->isChecked())
  156. {
  157. if(this->AspectRatio > 0)
  158. {
  159. this->HeightSpinBox->setValue(static_cast<int>(this->WidthSpinBox->value()/this->AspectRatio));
  160. }
  161. else
  162. {
  163. QString message = QString("Aspect ratio: ") + this->AspectRatio +\
  164. QString(" is invalid. Check widget dimensions.");
  165. QMessageBox::warning(q, "Invalid aspect ratio", message, QMessageBox::Ok);
  166. }
  167. }
  168. }
  169. //-----------------------------------------------------------------------------
  170. void ctkScreenshotDialogPrivate::onHeightEdited()
  171. {
  172. if(this->LockAspectToolButton->isChecked())
  173. {
  174. this->WidthSpinBox->setValue(static_cast<int>(this->HeightSpinBox->value()*this->AspectRatio));
  175. }
  176. }
  177. //-----------------------------------------------------------------------------
  178. void ctkScreenshotDialog::enforceResolution(int width, int height)
  179. {
  180. Q_D(ctkScreenshotDialog);
  181. d->OutputResolutionRadioButton->setChecked(true);
  182. d->useScalarFactor(true);
  183. d->ScaleFactorRadioButton->setEnabled(false);
  184. d->OutputResolutionRadioButton->setEnabled(false);
  185. d->ScaleFactorSpinBox->setEnabled(false);
  186. d->WidthSpinBox->setValue(width);
  187. d->HeightSpinBox->setValue(height);
  188. }
  189. //-----------------------------------------------------------------------------
  190. void ctkScreenshotDialog::enforceResolution(QSize size)
  191. {
  192. this->enforceResolution(size.width(), size.height());
  193. }
  194. //-----------------------------------------------------------------------------
  195. QSize ctkScreenshotDialog::widgetSize()
  196. {
  197. Q_D(ctkScreenshotDialog);
  198. QPixmap viewportPixmap = QPixmap::grabWidget(d->WidgetToGrab.data());
  199. return viewportPixmap.size();
  200. }
  201. //-----------------------------------------------------------------------------
  202. void ctkScreenshotDialog::enableTransparency(bool enable)
  203. {
  204. Q_D(ctkScreenshotDialog);
  205. d->AllowTransparency = enable;
  206. }
  207. //-----------------------------------------------------------------------------
  208. void ctkScreenshotDialogPrivate::saveScreenshot(int delayInSeconds)
  209. {
  210. Q_Q(ctkScreenshotDialog);
  211. if (this->WidgetToGrab.isNull())
  212. {
  213. return;
  214. }
  215. if (delayInSeconds <= 0)
  216. {
  217. q->instantScreenshot();
  218. return;
  219. }
  220. this->setWaitingForScreenshot(true);
  221. this->CountDownValue = delayInSeconds;
  222. this->CountDownTimer.start(1000);
  223. // Add 1ms to give time to set the countdown at 0.
  224. QTimer::singleShot(delayInSeconds * 1000 + 1, q, SLOT(instantScreenshot()));
  225. }
  226. //-----------------------------------------------------------------------------
  227. // ctkScreenshotDialog methods
  228. //-----------------------------------------------------------------------------
  229. ctkScreenshotDialog::ctkScreenshotDialog(QWidget* newParent)
  230. : Superclass(newParent)
  231. , d_ptr(new ctkScreenshotDialogPrivate(*this))
  232. {
  233. Q_D(ctkScreenshotDialog);
  234. d->init();
  235. }
  236. //-----------------------------------------------------------------------------
  237. ctkScreenshotDialog::~ctkScreenshotDialog()
  238. {
  239. }
  240. //-----------------------------------------------------------------------------
  241. void ctkScreenshotDialog::setWidgetToGrab(QWidget* newWidgetToGrab)
  242. {
  243. Q_D(ctkScreenshotDialog);
  244. d->OptionGroupBox->setEnabled(newWidgetToGrab != 0);
  245. d->CaptureButton->setEnabled(newWidgetToGrab != 0);
  246. d->WidgetToGrab = newWidgetToGrab;
  247. QSize curSize = this->widgetSize();
  248. d->HeightSpinBox->setValue(curSize.height());
  249. d->WidthSpinBox->setValue(curSize.width());
  250. }
  251. //-----------------------------------------------------------------------------
  252. QWidget* ctkScreenshotDialog::widgetToGrab() const
  253. {
  254. Q_D(const ctkScreenshotDialog);
  255. return d->WidgetToGrab.data();
  256. }
  257. //-----------------------------------------------------------------------------
  258. void ctkScreenshotDialog::setBaseFileName(const QString& newBaseName)
  259. {
  260. Q_D(ctkScreenshotDialog);
  261. d->ImageNameLineEdit->setText(newBaseName);
  262. }
  263. //-----------------------------------------------------------------------------
  264. QString ctkScreenshotDialog::baseFileName() const
  265. {
  266. Q_D(const ctkScreenshotDialog);
  267. return d->ImageNameLineEdit->text();
  268. }
  269. //-----------------------------------------------------------------------------
  270. void ctkScreenshotDialog::setDirectory(const QString& newDirectory)
  271. {
  272. Q_D(ctkScreenshotDialog);
  273. d->DirectoryPathLineEdit->setCurrentPath(newDirectory);
  274. }
  275. //-----------------------------------------------------------------------------
  276. QString ctkScreenshotDialog::directory()const
  277. {
  278. Q_D(const ctkScreenshotDialog);
  279. return d->DirectoryPathLineEdit->currentPath();
  280. }
  281. //-----------------------------------------------------------------------------
  282. void ctkScreenshotDialog::setDelay(int seconds)
  283. {
  284. Q_D(ctkScreenshotDialog);
  285. d->DelaySpinBox->setValue(seconds);
  286. }
  287. //-----------------------------------------------------------------------------
  288. int ctkScreenshotDialog::delay()const
  289. {
  290. Q_D(const ctkScreenshotDialog);
  291. return d->DelaySpinBox->value();
  292. }
  293. //-----------------------------------------------------------------------------
  294. void ctkScreenshotDialog::saveScreenshot()
  295. {
  296. Q_D(ctkScreenshotDialog);
  297. d->saveScreenshot(this->delay());
  298. }
  299. //-----------------------------------------------------------------------------
  300. void ctkScreenshotDialog::instantScreenshot()
  301. {
  302. Q_D(ctkScreenshotDialog);
  303. if (d->WidgetToGrab.isNull())
  304. {
  305. return;
  306. }
  307. QPixmap viewportPixmap = QPixmap::grabWidget(d->WidgetToGrab.data());
  308. if (d->isWaitingForScreenshot() && d->DelaySpinBox->value() != 0)
  309. {
  310. qApp->beep();
  311. }
  312. d->setWaitingForScreenshot(false);
  313. d->resetCountDownValue();
  314. // Rescale based on scale factor or output resolution specified
  315. QPixmap rescaledViewportPixmap = viewportPixmap;
  316. if(d->ScaleFactorRadioButton->isChecked())
  317. {
  318. rescaledViewportPixmap = viewportPixmap.scaled(
  319. viewportPixmap.size().width() * d->ScaleFactorSpinBox->value(),
  320. viewportPixmap.size().height() * d->ScaleFactorSpinBox->value());
  321. }
  322. else if(d->OutputResolutionRadioButton->isChecked())
  323. {
  324. rescaledViewportPixmap = viewportPixmap.scaled(
  325. d->WidthSpinBox->value(),
  326. d->HeightSpinBox->value());
  327. }
  328. QString filename = QString("%1/%2_%3.png").arg(d->DirectoryPathLineEdit->currentPath())
  329. .arg(d->ImageNameLineEdit->text())
  330. .arg(d->ImageVersionNumberSpinBox->value());
  331. // Check if file exists
  332. bool overwrite = d->OverwriteCheckBox->isChecked();
  333. if (QFile::exists(filename) && !overwrite)
  334. {
  335. int answer = QMessageBox::question(this, "Screen Capture",
  336. tr("File already exists. Overwrite ?"),
  337. QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No);
  338. if (answer == QMessageBox::YesToAll)
  339. {
  340. overwrite = true;
  341. d->OverwriteCheckBox->setChecked(true);
  342. }
  343. else if(answer == QMessageBox::Yes)
  344. {
  345. overwrite = true;
  346. }
  347. else
  348. {
  349. return;
  350. }
  351. }
  352. QImage img = rescaledViewportPixmap.toImage();
  353. if( !d->AllowTransparency &&
  354. img.hasAlphaChannel())
  355. {
  356. img = img.convertToFormat(QImage::Format_RGB32);
  357. }
  358. img.save(filename);
  359. d->ImageVersionNumberSpinBox->setValue(d->ImageVersionNumberSpinBox->value() + 1);
  360. }