CMakeSetupDialog.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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 <QMenu>
  24. #include <QMenuBar>
  25. #include <QDragEnterEvent>
  26. #include <QMimeData>
  27. #include <QUrl>
  28. #include <QShortcut>
  29. #include "QCMake.h"
  30. #include "QCMakeCacheView.h"
  31. #include "AddCacheEntry.h"
  32. QCMakeThread::QCMakeThread(QObject* p)
  33. : QThread(p), CMakeInstance(NULL)
  34. {
  35. }
  36. QCMake* QCMakeThread::cmakeInstance() const
  37. {
  38. return this->CMakeInstance;
  39. }
  40. void QCMakeThread::run()
  41. {
  42. this->CMakeInstance = new QCMake;
  43. // emit that this cmake thread is ready for use
  44. emit this->cmakeInitialized();
  45. this->exec();
  46. delete this->CMakeInstance;
  47. this->CMakeInstance = NULL;
  48. }
  49. CMakeSetupDialog::CMakeSetupDialog()
  50. : ExitAfterGenerate(true), CacheModified(false), CurrentState(Interrupting)
  51. {
  52. // create the GUI
  53. QSettings settings;
  54. settings.beginGroup("Settings/StartPath");
  55. int h = settings.value("Height", 500).toInt();
  56. int w = settings.value("Width", 700).toInt();
  57. this->resize(w, h);
  58. QWidget* cont = new QWidget(this);
  59. this->setupUi(cont);
  60. this->Splitter->setStretchFactor(0, 3);
  61. this->Splitter->setStretchFactor(1, 1);
  62. this->setCentralWidget(cont);
  63. this->ProgressBar->reset();
  64. this->RemoveEntry->setEnabled(false);
  65. this->AddEntry->setEnabled(false);
  66. QMenu* FileMenu = this->menuBar()->addMenu(tr("&File"));
  67. this->ReloadCacheAction = FileMenu->addAction(tr("&Reload Cache"));
  68. QObject::connect(this->ReloadCacheAction, SIGNAL(triggered(bool)),
  69. this, SLOT(doReloadCache()));
  70. this->DeleteCacheAction = FileMenu->addAction(tr("&Delete Cache"));
  71. QObject::connect(this->DeleteCacheAction, SIGNAL(triggered(bool)),
  72. this, SLOT(doDeleteCache()));
  73. this->ExitAction = FileMenu->addAction(tr("&Exit"));
  74. QObject::connect(this->ExitAction, SIGNAL(triggered(bool)),
  75. this, SLOT(close()));
  76. QMenu* ToolsMenu = this->menuBar()->addMenu(tr("&Tools"));
  77. this->ConfigureAction = ToolsMenu->addAction(tr("&Configure"));
  78. // prevent merging with Preferences menu item on Mac OS X
  79. this->ConfigureAction->setMenuRole(QAction::NoRole);
  80. QObject::connect(this->ConfigureAction, SIGNAL(triggered(bool)),
  81. this, SLOT(doConfigure()));
  82. this->GenerateAction = ToolsMenu->addAction(tr("&Generate"));
  83. QObject::connect(this->GenerateAction, SIGNAL(triggered(bool)),
  84. this, SLOT(doGenerate()));
  85. QMenu* HelpMenu = this->menuBar()->addMenu(tr("&Help"));
  86. QAction* a = HelpMenu->addAction(tr("About"));
  87. QObject::connect(a, SIGNAL(triggered(bool)),
  88. this, SLOT(doAbout()));
  89. a = HelpMenu->addAction(tr("Help"));
  90. QObject::connect(a, SIGNAL(triggered(bool)),
  91. this, SLOT(doHelp()));
  92. QShortcut* filterShortcut = new QShortcut(QKeySequence::Find, this);
  93. QObject::connect(filterShortcut, SIGNAL(activated()),
  94. this, SLOT(startSearch()));
  95. this->setAcceptDrops(true);
  96. // get the saved binary directories
  97. QStringList buildPaths = this->loadBuildPaths();
  98. this->BinaryDirectory->addItems(buildPaths);
  99. this->BinaryDirectory->setCompleter(new QCMakeFileCompleter(this, true));
  100. this->SourceDirectory->setCompleter(new QCMakeFileCompleter(this, true));
  101. // start the cmake worker thread
  102. this->CMakeThread = new QCMakeThread(this);
  103. QObject::connect(this->CMakeThread, SIGNAL(cmakeInitialized()),
  104. this, SLOT(initialize()), Qt::QueuedConnection);
  105. this->CMakeThread->start();
  106. this->enterState(ReadyConfigure);
  107. }
  108. void CMakeSetupDialog::initialize()
  109. {
  110. // now the cmake worker thread is running, lets make our connections to it
  111. QObject::connect(this->CMakeThread->cmakeInstance(),
  112. SIGNAL(propertiesChanged(const QCMakeCachePropertyList&)),
  113. this->CacheValues->cacheModel(),
  114. SLOT(setProperties(const QCMakeCachePropertyList&)));
  115. QObject::connect(this->ConfigureButton, SIGNAL(clicked(bool)),
  116. this, SLOT(doConfigure()));
  117. QObject::connect(this->CMakeThread->cmakeInstance(),
  118. SIGNAL(configureDone(int)),
  119. this, SLOT(finishConfigure(int)));
  120. QObject::connect(this->CMakeThread->cmakeInstance(),
  121. SIGNAL(generateDone(int)),
  122. this, SLOT(finishGenerate(int)));
  123. QObject::connect(this->GenerateButton, SIGNAL(clicked(bool)),
  124. this, SLOT(doGenerate()));
  125. QObject::connect(this->BrowseSourceDirectoryButton, SIGNAL(clicked(bool)),
  126. this, SLOT(doSourceBrowse()));
  127. QObject::connect(this->BrowseBinaryDirectoryButton, SIGNAL(clicked(bool)),
  128. this, SLOT(doBinaryBrowse()));
  129. QObject::connect(this->BinaryDirectory, SIGNAL(editTextChanged(QString)),
  130. this, SLOT(onBinaryDirectoryChanged(QString)));
  131. QObject::connect(this->SourceDirectory, SIGNAL(textChanged(QString)),
  132. this, SLOT(onSourceDirectoryChanged(QString)));
  133. QObject::connect(this->CMakeThread->cmakeInstance(),
  134. SIGNAL(sourceDirChanged(QString)),
  135. this, SLOT(updateSourceDirectory(QString)));
  136. QObject::connect(this->CMakeThread->cmakeInstance(),
  137. SIGNAL(progressChanged(QString, float)),
  138. this, SLOT(showProgress(QString,float)));
  139. QObject::connect(this->CMakeThread->cmakeInstance(),
  140. SIGNAL(errorMessage(QString)),
  141. this, SLOT(error(QString)));
  142. QObject::connect(this->CMakeThread->cmakeInstance(),
  143. SIGNAL(outputMessage(QString)),
  144. this, SLOT(message(QString)));
  145. QObject::connect(this->Advanced, SIGNAL(clicked(bool)),
  146. this->CacheValues, SLOT(setShowAdvanced(bool)));
  147. QObject::connect(this->Search, SIGNAL(textChanged(QString)),
  148. this->CacheValues, SLOT(setSearchFilter(QString)));
  149. QObject::connect(this->CMakeThread->cmakeInstance(),
  150. SIGNAL(generatorChanged(QString)),
  151. this, SLOT(updateGeneratorLabel(QString)));
  152. this->updateGeneratorLabel(QString());
  153. QObject::connect(this->CacheValues->cacheModel(),
  154. SIGNAL(dataChanged(QModelIndex,QModelIndex)),
  155. this, SLOT(setCacheModified()));
  156. QObject::connect(this->CacheValues->selectionModel(),
  157. SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
  158. this, SLOT(selectionChanged()));
  159. QObject::connect(this->RemoveEntry, SIGNAL(clicked(bool)),
  160. this, SLOT(removeSelectedCacheEntries()));
  161. QObject::connect(this->AddEntry, SIGNAL(clicked(bool)),
  162. this, SLOT(addCacheEntry()));
  163. if(!this->SourceDirectory->text().isEmpty() ||
  164. !this->BinaryDirectory->lineEdit()->text().isEmpty())
  165. {
  166. this->onSourceDirectoryChanged(this->SourceDirectory->text());
  167. this->onBinaryDirectoryChanged(this->BinaryDirectory->lineEdit()->text());
  168. }
  169. else
  170. {
  171. this->onBinaryDirectoryChanged(this->BinaryDirectory->lineEdit()->text());
  172. }
  173. }
  174. CMakeSetupDialog::~CMakeSetupDialog()
  175. {
  176. QSettings settings;
  177. settings.beginGroup("Settings/StartPath");
  178. settings.setValue("Height", this->height());
  179. settings.setValue("Width", this->width());
  180. // wait for thread to stop
  181. this->CMakeThread->quit();
  182. this->CMakeThread->wait(2000);
  183. }
  184. void CMakeSetupDialog::doConfigure()
  185. {
  186. if(this->CurrentState == Configuring)
  187. {
  188. // stop configure
  189. doInterrupt();
  190. return;
  191. }
  192. QString bindir = this->CMakeThread->cmakeInstance()->binaryDirectory();
  193. QDir dir(bindir);
  194. if(!dir.exists())
  195. {
  196. QString message = tr("Build directory does not exist, "
  197. "should I create it?")
  198. + "\n\n"
  199. + tr("Directory: ");
  200. message += bindir;
  201. QString title = tr("Create Directory");
  202. QMessageBox::StandardButton btn;
  203. btn = QMessageBox::information(this, title, message,
  204. QMessageBox::Yes | QMessageBox::No);
  205. if(btn == QMessageBox::No)
  206. {
  207. return;
  208. }
  209. dir.mkpath(".");
  210. }
  211. // prompt for generator if one doesn't exist
  212. if(this->CMakeThread->cmakeInstance()->generator().isEmpty())
  213. {
  214. this->promptForGenerator();
  215. }
  216. // remember path
  217. this->addBinaryPath(dir.absolutePath());
  218. this->enterState(Configuring);
  219. this->Output->clear();
  220. this->CacheValues->selectionModel()->clear();
  221. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  222. "setProperties", Qt::QueuedConnection,
  223. Q_ARG(QCMakeCachePropertyList,
  224. this->CacheValues->cacheModel()->properties()));
  225. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  226. "configure", Qt::QueuedConnection);
  227. }
  228. void CMakeSetupDialog::finishConfigure(int err)
  229. {
  230. if(0 == err && 0 == this->CacheValues->cacheModel()->newCount())
  231. {
  232. this->enterState(ReadyGenerate);
  233. }
  234. else
  235. {
  236. this->enterState(ReadyConfigure);
  237. this->CacheValues->scrollToTop();
  238. }
  239. if(err != 0)
  240. {
  241. QMessageBox::critical(this, tr("Error"),
  242. tr("Error in configuration process, project files may be invalid"),
  243. QMessageBox::Ok);
  244. }
  245. }
  246. void CMakeSetupDialog::finishGenerate(int err)
  247. {
  248. this->enterState(ReadyGenerate);
  249. if(err != 0)
  250. {
  251. QMessageBox::critical(this, tr("Error"),
  252. tr("Error in generation process, project files may be invalid"),
  253. QMessageBox::Ok);
  254. }
  255. }
  256. void CMakeSetupDialog::doGenerate()
  257. {
  258. if(this->CurrentState == Generating)
  259. {
  260. // stop generate
  261. doInterrupt();
  262. return;
  263. }
  264. this->enterState(Generating);
  265. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  266. "generate", Qt::QueuedConnection);
  267. }
  268. void CMakeSetupDialog::closeEvent(QCloseEvent* e)
  269. {
  270. // prompt for close if there are unsaved changes, and we're not busy
  271. if(this->CacheModified)
  272. {
  273. QString message = tr("You have changed options but not rebuilt, "
  274. "are you sure you want to exit?");
  275. QString title = tr("Confirm Exit");
  276. QMessageBox::StandardButton btn;
  277. btn = QMessageBox::critical(this, title, message,
  278. QMessageBox::Yes | QMessageBox::No);
  279. if(btn == QMessageBox::No)
  280. {
  281. e->ignore();
  282. }
  283. }
  284. // don't close if we're busy, unless the user really wants to
  285. if(this->CurrentState == Configuring)
  286. {
  287. QString message = "You are in the middle of a Configure.\n"
  288. "If you Exit now the configure information will be lost.\n"
  289. "Are you sure you want to Exit?";
  290. QString title = tr("Confirm Exit");
  291. QMessageBox::StandardButton btn;
  292. btn = QMessageBox::critical(this, title, message,
  293. QMessageBox::Yes | QMessageBox::No);
  294. if(btn == QMessageBox::No)
  295. {
  296. e->ignore();
  297. }
  298. else
  299. {
  300. this->doInterrupt();
  301. }
  302. }
  303. // let the generate finish
  304. if(this->CurrentState == Generating)
  305. {
  306. e->ignore();
  307. }
  308. }
  309. void CMakeSetupDialog::doHelp()
  310. {
  311. QString msg = tr("CMake is used to configure and generate build files for "
  312. "software projects. The basic steps for configuring a project are as "
  313. "follows:\r\n\r\n1. Select the source directory for the project. This should "
  314. "contain the CMakeLists.txt files for the project.\r\n\r\n2. Select the build "
  315. "directory for the project. This is the directory where the project will be "
  316. "built. It can be the same or a different directory than the source "
  317. "directory. For easy clean up, a separate build directory is recommended. "
  318. "CMake will create the directory if it does not exist.\r\n\r\n3. Once the "
  319. "source and binary directories are selected, it is time to press the "
  320. "Configure button. This will cause CMake to read all of the input files and "
  321. "discover all the variables used by the project. The first time a variable "
  322. "is displayed it will be in Red. Users should inspect red variables making "
  323. "sure the values are correct. For some projects the Configure process can "
  324. "be iterative, so continue to press the Configure button until there are no "
  325. "longer red entries.\r\n\r\n4. Once there are no longer red entries, you "
  326. "should click the Generate button. This will write the build files to the build "
  327. "directory.");
  328. QDialog dialog;
  329. dialog.setWindowTitle(tr("Help"));
  330. QVBoxLayout* l = new QVBoxLayout(&dialog);
  331. QLabel* lab = new QLabel(&dialog);
  332. l->addWidget(lab);
  333. lab->setText(msg);
  334. lab->setWordWrap(true);
  335. QDialogButtonBox* btns = new QDialogButtonBox(QDialogButtonBox::Ok,
  336. Qt::Horizontal, &dialog);
  337. QObject::connect(btns, SIGNAL(accepted()), &dialog, SLOT(accept()));
  338. l->addWidget(btns);
  339. dialog.exec();
  340. }
  341. void CMakeSetupDialog::doInterrupt()
  342. {
  343. this->enterState(Interrupting);
  344. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  345. "interrupt", Qt::QueuedConnection);
  346. }
  347. void CMakeSetupDialog::doSourceBrowse()
  348. {
  349. QString dir = QFileDialog::getExistingDirectory(this,
  350. tr("Enter Path to Source"), this->SourceDirectory->text());
  351. if(!dir.isEmpty())
  352. {
  353. this->setSourceDirectory(QDir::fromNativeSeparators(dir));
  354. }
  355. }
  356. void CMakeSetupDialog::updateSourceDirectory(const QString& dir)
  357. {
  358. if(this->SourceDirectory->text() != dir)
  359. {
  360. this->SourceDirectory->blockSignals(true);
  361. this->SourceDirectory->setText(dir);
  362. this->SourceDirectory->blockSignals(false);
  363. }
  364. }
  365. void CMakeSetupDialog::doBinaryBrowse()
  366. {
  367. QString dir = QFileDialog::getExistingDirectory(this,
  368. tr("Enter Path to Build"), this->BinaryDirectory->currentText());
  369. if(!dir.isEmpty() && dir != this->BinaryDirectory->currentText())
  370. {
  371. this->setBinaryDirectory(QDir::fromNativeSeparators(dir));
  372. }
  373. }
  374. void CMakeSetupDialog::setBinaryDirectory(const QString& dir)
  375. {
  376. this->BinaryDirectory->setEditText(dir);
  377. }
  378. void CMakeSetupDialog::onSourceDirectoryChanged(const QString& dir)
  379. {
  380. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  381. "setSourceDirectory", Qt::QueuedConnection, Q_ARG(QString, dir));
  382. }
  383. void CMakeSetupDialog::onBinaryDirectoryChanged(const QString& dir)
  384. {
  385. this->CacheModified = false;
  386. this->CacheValues->cacheModel()->clear();
  387. this->Output->clear();
  388. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  389. "setBinaryDirectory", Qt::QueuedConnection, Q_ARG(QString, dir));
  390. }
  391. void CMakeSetupDialog::setSourceDirectory(const QString& dir)
  392. {
  393. this->SourceDirectory->setText(dir);
  394. }
  395. void CMakeSetupDialog::showProgress(const QString& /*msg*/, float percent)
  396. {
  397. this->ProgressBar->setValue(qRound(percent * 100));
  398. }
  399. void CMakeSetupDialog::error(const QString& message)
  400. {
  401. QStringList messages = message.split('\n');
  402. foreach(QString m, messages)
  403. {
  404. // make sure we escape html tags in the cmake messages
  405. m.replace(QString("&"), QString("&amp;"));
  406. m.replace(QString("<"), QString("&lt;"));
  407. m.replace(QString(">"), QString("&gt;"));
  408. this->Output->append(QString("<b><font color=red>%1</font></b>").arg(m));
  409. }
  410. }
  411. void CMakeSetupDialog::message(const QString& message)
  412. {
  413. QStringList messages = message.split('\n');
  414. foreach(QString m, messages)
  415. {
  416. // make sure we escape html tags in the cmake messages
  417. m.replace(QString("&"), QString("&amp;"));
  418. m.replace(QString("<"), QString("&lt;"));
  419. m.replace(QString(">"), QString("&gt;"));
  420. this->Output->append(m);
  421. }
  422. }
  423. void CMakeSetupDialog::setEnabledState(bool enabled)
  424. {
  425. // disable parts of the GUI during configure/generate
  426. this->CacheValues->cacheModel()->setEditEnabled(enabled);
  427. this->SourceDirectory->setEnabled(enabled);
  428. this->BrowseSourceDirectoryButton->setEnabled(enabled);
  429. this->BinaryDirectory->setEnabled(enabled);
  430. this->BrowseBinaryDirectoryButton->setEnabled(enabled);
  431. this->ReloadCacheAction->setEnabled(enabled);
  432. this->DeleteCacheAction->setEnabled(enabled);
  433. this->ExitAction->setEnabled(enabled);
  434. this->ConfigureAction->setEnabled(enabled);
  435. this->AddEntry->setEnabled(enabled);
  436. this->RemoveEntry->setEnabled(false); // let selection re-enable it
  437. }
  438. void CMakeSetupDialog::promptForGenerator()
  439. {
  440. QSettings settings;
  441. settings.beginGroup("Settings/StartPath");
  442. QString lastGen = settings.value("LastGenerator").toString();
  443. QStringList gens = this->CMakeThread->cmakeInstance()->availableGenerators();
  444. QDialog dialog;
  445. dialog.setWindowTitle(tr("Choose Generator"));
  446. QLabel* lab = new QLabel(&dialog);
  447. lab->setText(tr("Please select what build system you want CMake to generate files for.\n"
  448. "You should select the tool that you will use to build the project.\n"
  449. "Press OK once you have made your selection."));
  450. QComboBox* combo = new QComboBox(&dialog);
  451. combo->addItems(gens);
  452. int idx = combo->findText(lastGen);
  453. if(idx != -1)
  454. {
  455. combo->setCurrentIndex(idx);
  456. }
  457. QDialogButtonBox* btns = new QDialogButtonBox(QDialogButtonBox::Ok,
  458. Qt::Horizontal, &dialog);
  459. QObject::connect(btns, SIGNAL(accepted()), &dialog, SLOT(accept()));
  460. QVBoxLayout* l = new QVBoxLayout(&dialog);
  461. l->addWidget(lab);
  462. l->addWidget(combo);
  463. l->addWidget(btns);
  464. dialog.exec();
  465. lastGen = combo->currentText();
  466. settings.setValue("LastGenerator", lastGen);
  467. this->CMakeThread->cmakeInstance()->setGenerator(combo->currentText());
  468. }
  469. void CMakeSetupDialog::updateGeneratorLabel(const QString& gen)
  470. {
  471. QString str = tr("Current Generator: ");
  472. if(gen.isEmpty())
  473. {
  474. str += tr("None");
  475. }
  476. else
  477. {
  478. str += gen;
  479. }
  480. this->Generator->setText(str);
  481. }
  482. void CMakeSetupDialog::doReloadCache()
  483. {
  484. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  485. "reloadCache", Qt::QueuedConnection);
  486. }
  487. void CMakeSetupDialog::doDeleteCache()
  488. {
  489. QString title = tr("Delete Cache");
  490. QString message = "Are you sure you want to delete the cache?";
  491. QMessageBox::StandardButton btn;
  492. btn = QMessageBox::information(this, title, message,
  493. QMessageBox::Yes | QMessageBox::No);
  494. if(btn == QMessageBox::No)
  495. {
  496. return;
  497. }
  498. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  499. "deleteCache", Qt::QueuedConnection);
  500. }
  501. void CMakeSetupDialog::doAbout()
  502. {
  503. QString msg = "CMake\nwww.cmake.org";
  504. QDialog dialog;
  505. dialog.setWindowTitle(tr("About"));
  506. QVBoxLayout* l = new QVBoxLayout(&dialog);
  507. QLabel* lab = new QLabel(&dialog);
  508. l->addWidget(lab);
  509. lab->setText(msg);
  510. lab->setWordWrap(true);
  511. QDialogButtonBox* btns = new QDialogButtonBox(QDialogButtonBox::Ok,
  512. Qt::Horizontal, &dialog);
  513. QObject::connect(btns, SIGNAL(accepted()), &dialog, SLOT(accept()));
  514. l->addWidget(btns);
  515. dialog.exec();
  516. }
  517. void CMakeSetupDialog::setExitAfterGenerate(bool b)
  518. {
  519. this->ExitAfterGenerate = b;
  520. }
  521. void CMakeSetupDialog::addBinaryPath(const QString& path)
  522. {
  523. QString cleanpath = QDir::cleanPath(path);
  524. // update UI
  525. this->BinaryDirectory->blockSignals(true);
  526. int idx = this->BinaryDirectory->findText(cleanpath);
  527. if(idx != -1)
  528. {
  529. this->BinaryDirectory->removeItem(idx);
  530. }
  531. this->BinaryDirectory->insertItem(0, cleanpath);
  532. this->BinaryDirectory->setCurrentIndex(0);
  533. this->BinaryDirectory->blockSignals(false);
  534. // save to registry
  535. QStringList buildPaths = this->loadBuildPaths();
  536. buildPaths.removeAll(cleanpath);
  537. buildPaths.prepend(cleanpath);
  538. this->saveBuildPaths(buildPaths);
  539. }
  540. void CMakeSetupDialog::dragEnterEvent(QDragEnterEvent* e)
  541. {
  542. if(!(this->CurrentState == ReadyConfigure ||
  543. this->CurrentState == ReadyGenerate))
  544. {
  545. e->ignore();
  546. return;
  547. }
  548. const QMimeData* dat = e->mimeData();
  549. QList<QUrl> urls = dat->urls();
  550. QString file = urls.count() ? urls[0].toLocalFile() : QString();
  551. if(!file.isEmpty() &&
  552. (file.endsWith("CMakeCache.txt", Qt::CaseInsensitive) ||
  553. file.endsWith("CMakeLists.txt", Qt::CaseInsensitive) ) )
  554. {
  555. e->accept();
  556. }
  557. else
  558. {
  559. e->ignore();
  560. }
  561. }
  562. void CMakeSetupDialog::dropEvent(QDropEvent* e)
  563. {
  564. if(!(this->CurrentState == ReadyConfigure ||
  565. this->CurrentState == ReadyGenerate))
  566. {
  567. return;
  568. }
  569. const QMimeData* dat = e->mimeData();
  570. QList<QUrl> urls = dat->urls();
  571. QString file = urls.count() ? urls[0].toLocalFile() : QString();
  572. if(file.endsWith("CMakeCache.txt", Qt::CaseInsensitive))
  573. {
  574. QFileInfo info(file);
  575. if(this->CMakeThread->cmakeInstance()->binaryDirectory() != info.absolutePath())
  576. {
  577. this->setBinaryDirectory(info.absolutePath());
  578. }
  579. }
  580. else if(file.endsWith("CMakeLists.txt", Qt::CaseInsensitive))
  581. {
  582. QFileInfo info(file);
  583. if(this->CMakeThread->cmakeInstance()->binaryDirectory() != info.absolutePath())
  584. {
  585. this->setSourceDirectory(info.absolutePath());
  586. this->setBinaryDirectory(info.absolutePath());
  587. }
  588. }
  589. }
  590. QStringList CMakeSetupDialog::loadBuildPaths()
  591. {
  592. QSettings settings;
  593. settings.beginGroup("Settings/StartPath");
  594. QStringList buildPaths;
  595. for(int i=0; i<10; i++)
  596. {
  597. QString p = settings.value(QString("WhereBuild%1").arg(i)).toString();
  598. if(!p.isEmpty())
  599. {
  600. buildPaths.append(p);
  601. }
  602. }
  603. return buildPaths;
  604. }
  605. void CMakeSetupDialog::saveBuildPaths(const QStringList& paths)
  606. {
  607. QSettings settings;
  608. settings.beginGroup("Settings/StartPath");
  609. int num = paths.count();
  610. if(num > 10)
  611. {
  612. num = 10;
  613. }
  614. for(int i=0; i<num; i++)
  615. {
  616. settings.setValue(QString("WhereBuild%1").arg(i), paths[i]);
  617. }
  618. }
  619. void CMakeSetupDialog::setCacheModified()
  620. {
  621. this->CacheModified = true;
  622. this->enterState(ReadyConfigure);
  623. }
  624. void CMakeSetupDialog::removeSelectedCacheEntries()
  625. {
  626. QModelIndexList idxs = this->CacheValues->selectionModel()->selectedRows();
  627. QList<QPersistentModelIndex> pidxs;
  628. foreach(QModelIndex i, idxs)
  629. {
  630. pidxs.append(i);
  631. }
  632. foreach(QPersistentModelIndex pi, pidxs)
  633. {
  634. this->CacheValues->model()->removeRow(pi.row());
  635. }
  636. }
  637. void CMakeSetupDialog::selectionChanged()
  638. {
  639. QModelIndexList idxs = this->CacheValues->selectionModel()->selectedRows();
  640. if(idxs.count() &&
  641. (this->CurrentState == ReadyConfigure ||
  642. this->CurrentState == ReadyGenerate) )
  643. {
  644. this->RemoveEntry->setEnabled(true);
  645. }
  646. else
  647. {
  648. this->RemoveEntry->setEnabled(false);
  649. }
  650. }
  651. void CMakeSetupDialog::enterState(CMakeSetupDialog::State s)
  652. {
  653. if(s == this->CurrentState)
  654. {
  655. return;
  656. }
  657. this->CurrentState = s;
  658. if(s == Interrupting)
  659. {
  660. this->ConfigureButton->setEnabled(false);
  661. this->GenerateButton->setEnabled(false);
  662. }
  663. else if(s == Configuring)
  664. {
  665. this->Output->clear();
  666. this->setEnabledState(false);
  667. this->GenerateButton->setEnabled(false);
  668. this->GenerateAction->setEnabled(false);
  669. this->ConfigureButton->setText(tr("Stop"));
  670. }
  671. else if(s == Generating)
  672. {
  673. this->CacheModified = false;
  674. this->Output->clear();
  675. this->setEnabledState(false);
  676. this->ConfigureButton->setEnabled(false);
  677. this->GenerateAction->setEnabled(false);
  678. this->GenerateButton->setText(tr("Stop"));
  679. }
  680. else if(s == ReadyConfigure)
  681. {
  682. this->ProgressBar->reset();
  683. this->setEnabledState(true);
  684. this->GenerateButton->setEnabled(false);
  685. this->GenerateAction->setEnabled(false);
  686. this->ConfigureButton->setEnabled(true);
  687. this->ConfigureButton->setText(tr("Configure"));
  688. this->GenerateButton->setText(tr("Generate"));
  689. }
  690. else if(s == ReadyGenerate)
  691. {
  692. this->ProgressBar->reset();
  693. this->setEnabledState(true);
  694. this->GenerateButton->setEnabled(true);
  695. this->GenerateAction->setEnabled(true);
  696. this->ConfigureButton->setEnabled(true);
  697. this->ConfigureButton->setText(tr("Configure"));
  698. this->GenerateButton->setText(tr("Generate"));
  699. }
  700. }
  701. void CMakeSetupDialog::addCacheEntry()
  702. {
  703. QDialog dialog(this);
  704. dialog.resize(400, 200);
  705. dialog.setWindowTitle(tr("Add Cache Entry"));
  706. QVBoxLayout* l = new QVBoxLayout(&dialog);
  707. AddCacheEntry* w = new AddCacheEntry(&dialog);
  708. QDialogButtonBox* btns = new QDialogButtonBox(
  709. QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
  710. Qt::Horizontal, &dialog);
  711. QObject::connect(btns, SIGNAL(accepted()), &dialog, SLOT(accept()));
  712. QObject::connect(btns, SIGNAL(rejected()), &dialog, SLOT(reject()));
  713. l->addWidget(w);
  714. l->addStretch();
  715. l->addWidget(btns);
  716. if(QDialog::Accepted == dialog.exec())
  717. {
  718. QCMakeCacheModel* m = this->CacheValues->cacheModel();
  719. m->insertRows(0, 1, QModelIndex());
  720. m->setData(m->index(0, 0), w->type(), QCMakeCacheModel::TypeRole);
  721. m->setData(m->index(0, 0), w->name(), Qt::DisplayRole);
  722. m->setData(m->index(0, 0), w->description(), QCMakeCacheModel::HelpRole);
  723. m->setData(m->index(0, 0), 0, QCMakeCacheModel::AdvancedRole);
  724. if(w->type() == QCMakeCacheProperty::BOOL)
  725. {
  726. m->setData(m->index(0, 1), w->value().toBool() ?
  727. Qt::Checked : Qt::Unchecked, Qt::CheckStateRole);
  728. }
  729. else
  730. {
  731. m->setData(m->index(0, 1), w->value(), Qt::DisplayRole);
  732. }
  733. }
  734. }
  735. void CMakeSetupDialog::startSearch()
  736. {
  737. this->Search->setFocus(Qt::OtherFocusReason);
  738. this->Search->selectAll();
  739. }