1
0

CMakeSetupDialog.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "CMakeSetupDialog.h"
  14. #include <QFileDialog>
  15. #include <QThread>
  16. #include <QProgressBar>
  17. #include <QMessageBox>
  18. #include <QStatusBar>
  19. #include <QToolButton>
  20. #include <QDialogButtonBox>
  21. #include <QCloseEvent>
  22. #include "QCMake.h"
  23. #include "QCMakeCacheView.h"
  24. // QCMake instance on a thread
  25. class QCMakeThread : public QThread
  26. {
  27. public:
  28. QCMakeThread(QObject* p) : QThread(p) { }
  29. QCMake* CMakeInstance;
  30. protected:
  31. virtual void run()
  32. {
  33. this->CMakeInstance = new QCMake;
  34. this->exec();
  35. delete this->CMakeInstance;
  36. }
  37. };
  38. CMakeSetupDialog::CMakeSetupDialog()
  39. {
  40. // create the GUI
  41. this->resize(700, 500);
  42. QWidget* cont = new QWidget(this);
  43. this->setupUi(cont);
  44. this->Splitter->setStretchFactor(0, 2);
  45. this->Splitter->setStretchFactor(1, 1);
  46. this->setCentralWidget(cont);
  47. this->ProgressBar = new QProgressBar();
  48. this->ProgressBar->setRange(0,100);
  49. this->InterruptButton = new QToolButton();
  50. this->InterruptButton->setEnabled(false);
  51. this->InterruptButton->setIcon(
  52. this->style()->standardPixmap(QStyle::SP_DialogCancelButton));
  53. this->statusBar()->addPermanentWidget(this->InterruptButton);
  54. this->statusBar()->addPermanentWidget(this->ProgressBar);
  55. // start the cmake worker thread
  56. this->CMakeThread = new QCMakeThread(this);
  57. // TODO does this guarantee the QCMake instance is created before initialize is called?
  58. QObject::connect(this->CMakeThread, SIGNAL(started()),
  59. this, SLOT(initialize()));
  60. this->CMakeThread->start();
  61. }
  62. void CMakeSetupDialog::initialize()
  63. {
  64. // now the cmake worker thread is running, lets make our connections to it
  65. QObject::connect(this->CMakeThread->CMakeInstance,
  66. SIGNAL(propertiesChanged(const QCMakeCachePropertyList&)),
  67. this->CacheValues->cacheModel(),
  68. SLOT(setProperties(const QCMakeCachePropertyList&)));
  69. QObject::connect(this->ConfigureButton, SIGNAL(clicked(bool)),
  70. this, SLOT(doConfigure()));
  71. QObject::connect(this->CMakeThread->CMakeInstance, SIGNAL(configureDone(int)),
  72. this, SLOT(finishConfigure(int)));
  73. QObject::connect(this->CMakeThread->CMakeInstance, SIGNAL(generateDone(int)),
  74. this, SLOT(finishGenerate(int)));
  75. QObject::connect(this->GenerateButton, SIGNAL(clicked(bool)),
  76. this, SLOT(doOk()));
  77. QObject::connect(this->CancelButton, SIGNAL(clicked(bool)),
  78. this, SLOT(close()));
  79. QObject::connect(this->BrowseSourceDirectoryButton, SIGNAL(clicked(bool)),
  80. this, SLOT(doSourceBrowse()));
  81. QObject::connect(this->BrowseBinaryDirectoryButton, SIGNAL(clicked(bool)),
  82. this, SLOT(doBinaryBrowse()));
  83. QObject::connect(this->BinaryDirectory, SIGNAL(editTextChanged(QString)),
  84. this->CMakeThread->CMakeInstance, SLOT(setBinaryDirectory(QString)));
  85. QObject::connect(this->SourceDirectory, SIGNAL(textChanged(QString)),
  86. this->CMakeThread->CMakeInstance, SLOT(setSourceDirectory(QString)));
  87. QObject::connect(this->CMakeThread->CMakeInstance, SIGNAL(sourceDirChanged(QString)),
  88. this, SLOT(updateSourceDirectory(QString)));
  89. QObject::connect(this->CMakeThread->CMakeInstance, SIGNAL(progressChanged(QString, float)),
  90. this, SLOT(showProgress(QString,float)));
  91. QObject::connect(this->CMakeThread->CMakeInstance, SIGNAL(error(QString, QString, bool*)),
  92. this, SLOT(error(QString,QString,bool*)), Qt::BlockingQueuedConnection);
  93. QObject::connect(this->InterruptButton, SIGNAL(clicked(bool)),
  94. this->CMakeThread->CMakeInstance, SLOT(interrupt()));
  95. QObject::connect(this->InterruptButton, SIGNAL(clicked(bool)),
  96. this, SLOT(doInterrupt()));
  97. QObject::connect(this->CMakeThread->CMakeInstance, SIGNAL(outputMessage(QString)),
  98. this->Output, SLOT(append(QString)));
  99. QObject::connect(this->HelpButton, SIGNAL(clicked(bool)),
  100. this, SLOT(doHelp()));
  101. }
  102. CMakeSetupDialog::~CMakeSetupDialog()
  103. {
  104. // wait for thread to stop
  105. this->CMakeThread->quit();
  106. this->CMakeThread->wait();
  107. }
  108. void CMakeSetupDialog::doConfigure()
  109. {
  110. QDir dir(this->BinaryDirectory->currentText());
  111. if(!dir.exists())
  112. {
  113. QString message = tr("Build directory does not exist, should I create it?\n\n"
  114. "Directory: ");
  115. message += this->BinaryDirectory->currentText();
  116. QString title = tr("Create Directory");
  117. QMessageBox::StandardButton btn =
  118. QMessageBox::information(this, title, message, QMessageBox::Yes | QMessageBox::No);
  119. if(btn == QMessageBox::No)
  120. {
  121. return;
  122. }
  123. dir.mkpath(".");
  124. }
  125. this->InterruptButton->setEnabled(true);
  126. this->setEnabledState(false);
  127. this->Output->clear();
  128. QMetaObject::invokeMethod(this->CMakeThread->CMakeInstance,
  129. "setProperties", Qt::QueuedConnection,
  130. Q_ARG(QCMakeCachePropertyList,
  131. this->CacheValues->cacheModel()->properties()));
  132. QMetaObject::invokeMethod(this->CMakeThread->CMakeInstance,
  133. "configure", Qt::QueuedConnection);
  134. }
  135. void CMakeSetupDialog::finishConfigure(int error)
  136. {
  137. this->InterruptButton->setEnabled(false);
  138. this->setEnabledState(true);
  139. this->ProgressBar->reset();
  140. this->statusBar()->showMessage(tr("Configure Done"), 2000);
  141. if(error != 0)
  142. {
  143. QMessageBox::critical(this, tr("Error"),
  144. tr("Error in configuration process, project files may be invalid"),
  145. QMessageBox::Ok);
  146. }
  147. }
  148. void CMakeSetupDialog::finishGenerate(int error)
  149. {
  150. this->InterruptButton->setEnabled(false);
  151. this->setEnabledState(true);
  152. this->ProgressBar->reset();
  153. this->statusBar()->showMessage(tr("Generate Done"), 2000);
  154. if(error != 0)
  155. {
  156. QMessageBox::critical(this, tr("Error"),
  157. tr("Error in generation process, project files may be invalid"),
  158. QMessageBox::Ok);
  159. }
  160. else
  161. {
  162. QApplication::quit();
  163. }
  164. }
  165. void CMakeSetupDialog::doOk()
  166. {
  167. this->InterruptButton->setEnabled(true);
  168. this->setEnabledState(false);
  169. this->Output->clear();
  170. QMetaObject::invokeMethod(this->CMakeThread->CMakeInstance,
  171. "generate", Qt::QueuedConnection);
  172. }
  173. void CMakeSetupDialog::closeEvent(QCloseEvent* e)
  174. {
  175. if(this->CacheValues->cacheModel()->isDirty())
  176. {
  177. QString message = tr("You have changed options but not rebuilt, "
  178. "are you sure you want to exit?");
  179. QString title = tr("Confirm Exit");
  180. QMessageBox::StandardButton btn =
  181. QMessageBox::critical(this, title, message, QMessageBox::Yes | QMessageBox::No);
  182. if(btn == QMessageBox::No)
  183. {
  184. e->ignore();
  185. }
  186. }
  187. }
  188. void CMakeSetupDialog::doHelp()
  189. {
  190. QString msg = tr("CMake is used to configure and generate build files for"
  191. "software projects. The basic steps for configuring a project are as"
  192. "follows:\r\n\r\n1. Select the source directory for the project. This should"
  193. "contain the CMakeLists.txt files for the project.\r\n\r\n2. Select the build"
  194. "directory for the project. This is the directory where the project will be"
  195. "built. It can be the same or a different directory than the source"
  196. "directory. For easy clean up, a separate build directory is recommended."
  197. "CMake will create the directory if it does not exist.\r\n\r\n3. Once the"
  198. "source and binary directories are selected, it is time to press the"
  199. "Configure button. This will cause CMake to read all of the input files and"
  200. "discover all the variables used by the project. The first time a variable"
  201. "is displayed it will be in Red. Users should inspect red variables making"
  202. "sure the values are correct. For some projects the Configure process can"
  203. "be iterative, so continue to press the Configure button until there are no"
  204. "longer red entries.\r\n\r\n4. Once there are no longer red entries, you"
  205. "should click the OK button. This will write the build files to the build"
  206. "directory and exit CMake.");
  207. QDialog dialog;
  208. QVBoxLayout* l = new QVBoxLayout(&dialog);
  209. QLabel* label = new QLabel(&dialog);
  210. l->addWidget(label);
  211. label->setText(msg);
  212. label->setWordWrap(true);
  213. QDialogButtonBox* btns = new QDialogButtonBox(QDialogButtonBox::Ok,
  214. Qt::Horizontal, &dialog);
  215. QObject::connect(btns, SIGNAL(accepted()), &dialog, SLOT(accept()));
  216. l->addWidget(btns);
  217. dialog.exec();
  218. }
  219. void CMakeSetupDialog::doInterrupt()
  220. {
  221. this->InterruptButton->setEnabled(false);
  222. this->statusBar()->showMessage(tr("Interrupting..."));
  223. }
  224. void CMakeSetupDialog::doSourceBrowse()
  225. {
  226. QString dir = QFileDialog::getExistingDirectory(this,
  227. tr("Enter Path to Source"), this->SourceDirectory->text());
  228. if(!dir.isEmpty())
  229. {
  230. this->SourceDirectory->setText(dir);
  231. }
  232. }
  233. void CMakeSetupDialog::updateSourceDirectory(const QString& dir)
  234. {
  235. if(this->SourceDirectory->text() != dir)
  236. {
  237. this->SourceDirectory->setText(dir);
  238. }
  239. }
  240. void CMakeSetupDialog::doBinaryBrowse()
  241. {
  242. QString dir = QFileDialog::getExistingDirectory(this,
  243. tr("Enter Path to Build"), this->BinaryDirectory->currentText());
  244. if(!dir.isEmpty())
  245. {
  246. this->setBinaryDirectory(dir);
  247. }
  248. }
  249. void CMakeSetupDialog::setBinaryDirectory(const QString& dir)
  250. {
  251. if(dir != this->BinaryDirectory->currentText())
  252. {
  253. this->CacheValues->cacheModel()->setProperties(QCMakeCachePropertyList());
  254. this->Output->clear();
  255. this->BinaryDirectory->setEditText(dir);
  256. }
  257. }
  258. void CMakeSetupDialog::showProgress(const QString& msg, float percent)
  259. {
  260. this->statusBar()->showMessage(msg);
  261. this->ProgressBar->setValue(qRound(percent * 100));
  262. }
  263. void CMakeSetupDialog::error(const QString& title, const QString& message, bool* cancel)
  264. {
  265. QMessageBox::StandardButton btn =
  266. QMessageBox::critical(this, title, message, QMessageBox::Ok | QMessageBox::Cancel);
  267. if(btn == QMessageBox::Cancel)
  268. {
  269. *cancel = false;
  270. }
  271. }
  272. void CMakeSetupDialog::setEnabledState(bool enabled)
  273. {
  274. this->CacheValues->setEnabled(enabled);
  275. this->SourceDirectory->setEnabled(enabled);
  276. this->BrowseSourceDirectoryButton->setEnabled(enabled);
  277. this->BinaryDirectory->setEnabled(enabled);
  278. this->BrowseBinaryDirectoryButton->setEnabled(enabled);
  279. this->ConfigureButton->setEnabled(enabled);
  280. this->GenerateButton->setEnabled(enabled);
  281. this->CancelButton->setEnabled(enabled);
  282. this->HelpButton->setEnabled(enabled);
  283. }