CMakeSetupDialog.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 <QProgressBar>
  16. #include <QMessageBox>
  17. #include <QStatusBar>
  18. #include <QToolButton>
  19. #include <QDialogButtonBox>
  20. #include <QCloseEvent>
  21. #include <QCoreApplication>
  22. #include <QSettings>
  23. #include "QCMake.h"
  24. #include "QCMakeCacheView.h"
  25. QCMakeThread::QCMakeThread(QObject* p)
  26. : QThread(p), CMakeInstance(NULL)
  27. {
  28. }
  29. QCMake* QCMakeThread::cmakeInstance() const
  30. {
  31. return this->CMakeInstance;
  32. }
  33. void QCMakeThread::processEvents()
  34. {
  35. QCoreApplication::processEvents();
  36. }
  37. void QCMakeThread::run()
  38. {
  39. this->CMakeInstance = new QCMake;
  40. // make the cmake thread to process events it receives from the GUI thread
  41. QObject::connect(this->CMakeInstance, SIGNAL(progressChanged(QString, float)),
  42. this, SLOT(processEvents()), Qt::DirectConnection);
  43. QObject::connect(this->CMakeInstance, SIGNAL(outputMessage(QString)),
  44. this, SLOT(processEvents()), Qt::DirectConnection);
  45. // emit that this cmake thread is ready for use
  46. emit this->cmakeInitialized();
  47. this->exec();
  48. delete this->CMakeInstance;
  49. this->CMakeInstance = NULL;
  50. }
  51. CMakeSetupDialog::CMakeSetupDialog()
  52. {
  53. // create the GUI
  54. QSettings settings;
  55. settings.beginGroup("Settings/StartPath");
  56. int h = settings.value("Height", 500).toInt();
  57. int w = settings.value("Width", 700).toInt();
  58. this->resize(w, h);
  59. QWidget* cont = new QWidget(this);
  60. this->setupUi(cont);
  61. this->Splitter->setStretchFactor(0, 2);
  62. this->Splitter->setStretchFactor(1, 1);
  63. this->setCentralWidget(cont);
  64. this->ProgressBar = new QProgressBar();
  65. this->ProgressBar->setRange(0,100);
  66. this->InterruptButton = new QToolButton();
  67. this->InterruptButton->setEnabled(false);
  68. this->InterruptButton->setIcon(
  69. this->style()->standardPixmap(QStyle::SP_DialogCancelButton));
  70. this->statusBar()->addPermanentWidget(this->InterruptButton);
  71. this->statusBar()->addPermanentWidget(this->ProgressBar);
  72. // start the cmake worker thread
  73. this->CMakeThread = new QCMakeThread(this);
  74. QObject::connect(this->CMakeThread, SIGNAL(cmakeInitialized()),
  75. this, SLOT(initialize()), Qt::QueuedConnection);
  76. this->CMakeThread->start();
  77. }
  78. void CMakeSetupDialog::initialize()
  79. {
  80. // now the cmake worker thread is running, lets make our connections to it
  81. QObject::connect(this->CMakeThread->cmakeInstance(),
  82. SIGNAL(propertiesChanged(const QCMakeCachePropertyList&)),
  83. this->CacheValues->cacheModel(),
  84. SLOT(setProperties(const QCMakeCachePropertyList&)));
  85. QObject::connect(this->ConfigureButton, SIGNAL(clicked(bool)),
  86. this, SLOT(doConfigure()));
  87. QObject::connect(this->CMakeThread->cmakeInstance(),
  88. SIGNAL(configureDone(int)),
  89. this, SLOT(finishConfigure(int)));
  90. QObject::connect(this->CMakeThread->cmakeInstance(),
  91. SIGNAL(generateDone(int)),
  92. this, SLOT(finishGenerate(int)));
  93. QObject::connect(this->GenerateButton, SIGNAL(clicked(bool)),
  94. this, SLOT(doOk()));
  95. QObject::connect(this->CancelButton, SIGNAL(clicked(bool)),
  96. this, SLOT(close()));
  97. QObject::connect(this->BrowseSourceDirectoryButton, SIGNAL(clicked(bool)),
  98. this, SLOT(doSourceBrowse()));
  99. QObject::connect(this->BrowseBinaryDirectoryButton, SIGNAL(clicked(bool)),
  100. this, SLOT(doBinaryBrowse()));
  101. QObject::connect(this->BinaryDirectory, SIGNAL(editTextChanged(QString)),
  102. this, SLOT(setBinaryDirectory(QString)));
  103. QObject::connect(this->SourceDirectory, SIGNAL(textChanged(QString)),
  104. this->CMakeThread->cmakeInstance(),
  105. SLOT(setSourceDirectory(QString)));
  106. QObject::connect(this->CMakeThread->cmakeInstance(),
  107. SIGNAL(sourceDirChanged(QString)),
  108. this, SLOT(updateSourceDirectory(QString)));
  109. QObject::connect(this->CMakeThread->cmakeInstance(),
  110. SIGNAL(progressChanged(QString, float)),
  111. this, SLOT(showProgress(QString,float)));
  112. QObject::connect(this->CMakeThread->cmakeInstance(),
  113. SIGNAL(error(QString, QString, bool*)),
  114. this, SLOT(error(QString,QString,bool*)),
  115. Qt::BlockingQueuedConnection);
  116. QObject::connect(this->InterruptButton, SIGNAL(clicked(bool)),
  117. this->CMakeThread->cmakeInstance(), SLOT(interrupt()));
  118. QObject::connect(this->InterruptButton, SIGNAL(clicked(bool)),
  119. this, SLOT(doInterrupt()));
  120. QObject::connect(this->CMakeThread->cmakeInstance(),
  121. SIGNAL(outputMessage(QString)),
  122. this->Output, SLOT(append(QString)));
  123. QObject::connect(this->HelpButton, SIGNAL(clicked(bool)),
  124. this, SLOT(doHelp()));
  125. QObject::connect(this->Advanced, SIGNAL(clicked(bool)),
  126. this->CacheValues, SLOT(setShowAdvanced(bool)));
  127. QObject::connect(this->Search, SIGNAL(textChanged(QString)),
  128. this->CacheValues, SLOT(setSearchFilter(QString)));
  129. QStringList gens = this->CMakeThread->cmakeInstance()->availableGenerators();
  130. this->Generators->addItems(gens);
  131. // get the saved binary directories
  132. QSettings settings;
  133. settings.beginGroup("Settings/StartPath");
  134. QStringList buildPaths;
  135. for(int i=0; i<10; i++)
  136. {
  137. QString p = settings.value(QString("WhereBuild%1").arg(i)).toString();
  138. if(!p.isEmpty())
  139. {
  140. buildPaths.append(p);
  141. }
  142. }
  143. this->BinaryDirectory->addItems(buildPaths);
  144. QString lastGen = settings.value("LastGenerator").toString();
  145. int idx = this->Generators->findText(lastGen);
  146. if(idx != -1)
  147. {
  148. this->Generators->setCurrentIndex(idx);
  149. }
  150. }
  151. CMakeSetupDialog::~CMakeSetupDialog()
  152. {
  153. QSettings settings;
  154. settings.beginGroup("Settings/StartPath");
  155. settings.setValue("Height", this->height());
  156. settings.setValue("Width", this->width());
  157. // wait for thread to stop
  158. this->CMakeThread->quit();
  159. this->CMakeThread->wait();
  160. }
  161. void CMakeSetupDialog::doConfigure()
  162. {
  163. QDir dir(this->BinaryDirectory->currentText());
  164. if(!dir.exists())
  165. {
  166. QString message = tr("Build directory does not exist, "
  167. "should I create it?\n\n"
  168. "Directory: ");
  169. message += this->BinaryDirectory->currentText();
  170. QString title = tr("Create Directory");
  171. QMessageBox::StandardButton btn;
  172. btn = QMessageBox::information(this, title, message,
  173. QMessageBox::Yes | QMessageBox::No);
  174. if(btn == QMessageBox::No)
  175. {
  176. return;
  177. }
  178. dir.mkpath(".");
  179. }
  180. /*
  181. // prompt for generator if one doesn't exist
  182. if(this->CMakeThread->cmakeInstance()->generator().isEmpty())
  183. {
  184. this->promptForGenerator();
  185. }
  186. */
  187. this->CMakeThread->cmakeInstance()->setGenerator(this->Generators->currentText());
  188. this->InterruptButton->setEnabled(true);
  189. this->setEnabledState(false);
  190. this->Output->clear();
  191. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  192. "setProperties", Qt::QueuedConnection,
  193. Q_ARG(QCMakeCachePropertyList,
  194. this->CacheValues->cacheModel()->properties()));
  195. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  196. "configure", Qt::QueuedConnection);
  197. }
  198. void CMakeSetupDialog::finishConfigure(int err)
  199. {
  200. this->InterruptButton->setEnabled(false);
  201. this->setEnabledState(true);
  202. this->ProgressBar->reset();
  203. this->statusBar()->showMessage(tr("Configure Done"), 2000);
  204. if(err != 0)
  205. {
  206. QMessageBox::critical(this, tr("Error"),
  207. tr("Error in configuration process, project files may be invalid"),
  208. QMessageBox::Ok);
  209. }
  210. }
  211. void CMakeSetupDialog::finishGenerate(int err)
  212. {
  213. this->InterruptButton->setEnabled(false);
  214. this->setEnabledState(true);
  215. this->ProgressBar->reset();
  216. this->statusBar()->showMessage(tr("Generate Done"), 2000);
  217. if(err != 0)
  218. {
  219. QMessageBox::critical(this, tr("Error"),
  220. tr("Error in generation process, project files may be invalid"),
  221. QMessageBox::Ok);
  222. }
  223. else
  224. {
  225. QApplication::quit();
  226. }
  227. }
  228. void CMakeSetupDialog::doOk()
  229. {
  230. this->InterruptButton->setEnabled(true);
  231. this->setEnabledState(false);
  232. this->Output->clear();
  233. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  234. "generate", Qt::QueuedConnection);
  235. }
  236. void CMakeSetupDialog::closeEvent(QCloseEvent* e)
  237. {
  238. // don't close if we're busy
  239. if(this->InterruptButton->isEnabled())
  240. {
  241. e->ignore();
  242. }
  243. // prompt for close if there are unsaved changes
  244. if(this->CacheValues->cacheModel()->isDirty())
  245. {
  246. QString message = tr("You have changed options but not rebuilt, "
  247. "are you sure you want to exit?");
  248. QString title = tr("Confirm Exit");
  249. QMessageBox::StandardButton btn;
  250. btn = QMessageBox::critical(this, title, message,
  251. QMessageBox::Yes | QMessageBox::No);
  252. if(btn == QMessageBox::No)
  253. {
  254. e->ignore();
  255. }
  256. }
  257. }
  258. void CMakeSetupDialog::doHelp()
  259. {
  260. QString msg = tr("CMake is used to configure and generate build files for "
  261. "software projects. The basic steps for configuring a project are as "
  262. "follows:\r\n\r\n1. Select the source directory for the project. This should "
  263. "contain the CMakeLists.txt files for the project.\r\n\r\n2. Select the build "
  264. "directory for the project. This is the directory where the project will be "
  265. "built. It can be the same or a different directory than the source "
  266. "directory. For easy clean up, a separate build directory is recommended. "
  267. "CMake will create the directory if it does not exist.\r\n\r\n3. Once the "
  268. "source and binary directories are selected, it is time to press the "
  269. "Configure button. This will cause CMake to read all of the input files and "
  270. "discover all the variables used by the project. The first time a variable "
  271. "is displayed it will be in Red. Users should inspect red variables making "
  272. "sure the values are correct. For some projects the Configure process can "
  273. "be iterative, so continue to press the Configure button until there are no "
  274. "longer red entries.\r\n\r\n4. Once there are no longer red entries, you "
  275. "should click the OK button. This will write the build files to the build "
  276. "directory and exit CMake.");
  277. QDialog dialog;
  278. dialog.setWindowTitle(tr("CMakeSetup Help"));
  279. QVBoxLayout* l = new QVBoxLayout(&dialog);
  280. QLabel* lab = new QLabel(&dialog);
  281. l->addWidget(lab);
  282. lab->setText(msg);
  283. lab->setWordWrap(true);
  284. QDialogButtonBox* btns = new QDialogButtonBox(QDialogButtonBox::Ok,
  285. Qt::Horizontal, &dialog);
  286. QObject::connect(btns, SIGNAL(accepted()), &dialog, SLOT(accept()));
  287. l->addWidget(btns);
  288. dialog.exec();
  289. }
  290. void CMakeSetupDialog::doInterrupt()
  291. {
  292. this->InterruptButton->setEnabled(false);
  293. this->statusBar()->showMessage(tr("Interrupting..."));
  294. }
  295. void CMakeSetupDialog::doSourceBrowse()
  296. {
  297. QString dir = QFileDialog::getExistingDirectory(this,
  298. tr("Enter Path to Source"), this->SourceDirectory->text());
  299. if(!dir.isEmpty())
  300. {
  301. this->SourceDirectory->setText(dir);
  302. }
  303. }
  304. void CMakeSetupDialog::updateSourceDirectory(const QString& dir)
  305. {
  306. if(this->SourceDirectory->text() != dir)
  307. {
  308. this->SourceDirectory->setText(dir);
  309. }
  310. }
  311. void CMakeSetupDialog::doBinaryBrowse()
  312. {
  313. QString dir = QFileDialog::getExistingDirectory(this,
  314. tr("Enter Path to Build"), this->BinaryDirectory->currentText());
  315. if(!dir.isEmpty() && dir != this->BinaryDirectory->currentText())
  316. {
  317. this->setBinaryDirectory(dir);
  318. }
  319. }
  320. void CMakeSetupDialog::setBinaryDirectory(const QString& dir)
  321. {
  322. this->CacheValues->cacheModel()->clear();
  323. this->Output->clear();
  324. this->BinaryDirectory->setEditText(dir);
  325. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  326. "setBinaryDirectory", Qt::QueuedConnection, Q_ARG(QString, dir));
  327. }
  328. void CMakeSetupDialog::showProgress(const QString& msg, float percent)
  329. {
  330. this->statusBar()->showMessage(msg);
  331. this->ProgressBar->setValue(qRound(percent * 100));
  332. }
  333. void CMakeSetupDialog::error(const QString& title, const QString& message,
  334. bool* cancel)
  335. {
  336. QMessageBox::StandardButton btn;
  337. btn = QMessageBox::critical(this, title, message,
  338. QMessageBox::Ok | QMessageBox::Cancel);
  339. if(btn == QMessageBox::Cancel)
  340. {
  341. *cancel = false;
  342. }
  343. }
  344. void CMakeSetupDialog::setEnabledState(bool enabled)
  345. {
  346. this->CacheValues->setEnabled(enabled);
  347. this->SourceDirectory->setEnabled(enabled);
  348. this->BrowseSourceDirectoryButton->setEnabled(enabled);
  349. this->BinaryDirectory->setEnabled(enabled);
  350. this->BrowseBinaryDirectoryButton->setEnabled(enabled);
  351. this->ConfigureButton->setEnabled(enabled);
  352. this->GenerateButton->setEnabled(enabled);
  353. this->CancelButton->setEnabled(enabled);
  354. }
  355. /*
  356. void CMakeSetupDialog::promptForGenerator()
  357. {
  358. QStringList gens = this->CMakeThread->cmakeInstance()->availableGenerators();
  359. QDialog dialog;
  360. dialog.setWindowTitle(tr("CMakeSetup choose generator"));
  361. QLabel* lab = new QLabel(&dialog);
  362. lab->setText(tr("Please select what build system you want CMake to generate files for.\n"
  363. "You should select the tool that you will use to build the project.\n"
  364. "Press OK once you have made your selection."));
  365. QComboBox* combo = new QComboBox(&dialog);
  366. combo->addItems(gens);
  367. QDialogButtonBox* btns = new QDialogButtonBox(QDialogButtonBox::Ok,
  368. Qt::Horizontal, &dialog);
  369. QObject::connect(btns, SIGNAL(accepted()), &dialog, SLOT(accept()));
  370. QVBoxLayout* l = new QVBoxLayout(&dialog);
  371. l->addWidget(lab);
  372. l->addWidget(combo);
  373. l->addWidget(btns);
  374. dialog.exec();
  375. this->CMakeThread->cmakeInstance()->setGenerator(combo->currentText());
  376. }
  377. */