ctkScreenshotDialog.cpp 13 KB

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