CMakeSetupDialog.cxx 24 KB

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