CMakeSetupDialog.cxx 25 KB

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