CMakeSetupDialog.cxx 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "CMakeSetupDialog.h"
  11. #include <QFileDialog>
  12. #include <QProgressBar>
  13. #include <QMessageBox>
  14. #include <QStatusBar>
  15. #include <QToolButton>
  16. #include <QDialogButtonBox>
  17. #include <QCloseEvent>
  18. #include <QCoreApplication>
  19. #include <QSettings>
  20. #include <QMenu>
  21. #include <QMenuBar>
  22. #include <QDragEnterEvent>
  23. #include <QMimeData>
  24. #include <QUrl>
  25. #include <QShortcut>
  26. #include <QMacInstallDialog.h>
  27. #include "QCMake.h"
  28. #include "QCMakeCacheView.h"
  29. #include "AddCacheEntry.h"
  30. #include "FirstConfigure.h"
  31. #include "cmVersion.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), ConfigureNeeded(true), CurrentState(Interrupting)
  51. {
  52. QString title = QString(tr("CMake %1"));
  53. title = title.arg(cmVersion::GetCMakeVersion());
  54. this->setWindowTitle(title);
  55. // create the GUI
  56. QSettings settings;
  57. settings.beginGroup("Settings/StartPath");
  58. int h = settings.value("Height", 500).toInt();
  59. int w = settings.value("Width", 700).toInt();
  60. this->resize(w, h);
  61. QWidget* cont = new QWidget(this);
  62. this->setupUi(cont);
  63. this->Splitter->setStretchFactor(0, 3);
  64. this->Splitter->setStretchFactor(1, 1);
  65. this->setCentralWidget(cont);
  66. this->ProgressBar->reset();
  67. this->RemoveEntry->setEnabled(false);
  68. this->AddEntry->setEnabled(false);
  69. QByteArray p = settings.value("SplitterSizes").toByteArray();
  70. this->Splitter->restoreState(p);
  71. bool groupView = settings.value("GroupView", false).toBool();
  72. this->setGroupedView(groupView);
  73. this->groupedCheck->setCheckState(groupView ? Qt::Checked : Qt::Unchecked);
  74. bool advancedView = settings.value("AdvancedView", false).toBool();
  75. this->setAdvancedView(advancedView);
  76. this->advancedCheck->setCheckState(advancedView?Qt::Checked : Qt::Unchecked);
  77. QMenu* FileMenu = this->menuBar()->addMenu(tr("&File"));
  78. this->ReloadCacheAction = FileMenu->addAction(tr("&Reload Cache"));
  79. QObject::connect(this->ReloadCacheAction, SIGNAL(triggered(bool)),
  80. this, SLOT(doReloadCache()));
  81. this->DeleteCacheAction = FileMenu->addAction(tr("&Delete Cache"));
  82. QObject::connect(this->DeleteCacheAction, SIGNAL(triggered(bool)),
  83. this, SLOT(doDeleteCache()));
  84. this->ExitAction = FileMenu->addAction(tr("E&xit"));
  85. QObject::connect(this->ExitAction, SIGNAL(triggered(bool)),
  86. this, SLOT(close()));
  87. QMenu* ToolsMenu = this->menuBar()->addMenu(tr("&Tools"));
  88. this->ConfigureAction = ToolsMenu->addAction(tr("&Configure"));
  89. // prevent merging with Preferences menu item on Mac OS X
  90. this->ConfigureAction->setMenuRole(QAction::NoRole);
  91. QObject::connect(this->ConfigureAction, SIGNAL(triggered(bool)),
  92. this, SLOT(doConfigure()));
  93. this->GenerateAction = ToolsMenu->addAction(tr("&Generate"));
  94. QObject::connect(this->GenerateAction, SIGNAL(triggered(bool)),
  95. this, SLOT(doGenerate()));
  96. QAction* showChangesAction = ToolsMenu->addAction(tr("&Show My Changes"));
  97. QObject::connect(showChangesAction, SIGNAL(triggered(bool)),
  98. this, SLOT(showUserChanges()));
  99. #if defined(Q_WS_MAC)
  100. this->InstallForCommandLineAction
  101. = ToolsMenu->addAction(tr("&Install For Command Line Use"));
  102. QObject::connect(this->InstallForCommandLineAction, SIGNAL(triggered(bool)),
  103. this, SLOT(doInstallForCommandLine()));
  104. #endif
  105. QMenu* OptionsMenu = this->menuBar()->addMenu(tr("&Options"));
  106. this->SuppressDevWarningsAction = OptionsMenu->addAction(tr("&Suppress dev Warnings (-Wno-dev)"));
  107. this->SuppressDevWarningsAction->setCheckable(true);
  108. QAction* debugAction = OptionsMenu->addAction(tr("&Debug Output"));
  109. debugAction->setCheckable(true);
  110. QObject::connect(debugAction, SIGNAL(toggled(bool)),
  111. this, SLOT(setDebugOutput(bool)));
  112. OptionsMenu->addSeparator();
  113. QAction* expandAction = OptionsMenu->addAction(tr("&Expand Grouped Entries"));
  114. QObject::connect(expandAction, SIGNAL(triggered(bool)),
  115. this->CacheValues, SLOT(expandAll()));
  116. QAction* collapseAction = OptionsMenu->addAction(tr("&Collapse Grouped Entries"));
  117. QObject::connect(collapseAction, SIGNAL(triggered(bool)),
  118. this->CacheValues, SLOT(collapseAll()));
  119. QMenu* HelpMenu = this->menuBar()->addMenu(tr("&Help"));
  120. QAction* a = HelpMenu->addAction(tr("About"));
  121. QObject::connect(a, SIGNAL(triggered(bool)),
  122. this, SLOT(doAbout()));
  123. a = HelpMenu->addAction(tr("Help"));
  124. QObject::connect(a, SIGNAL(triggered(bool)),
  125. this, SLOT(doHelp()));
  126. QShortcut* filterShortcut = new QShortcut(QKeySequence::Find, this);
  127. QObject::connect(filterShortcut, SIGNAL(activated()),
  128. this, SLOT(startSearch()));
  129. this->setAcceptDrops(true);
  130. // get the saved binary directories
  131. QStringList buildPaths = this->loadBuildPaths();
  132. this->BinaryDirectory->addItems(buildPaths);
  133. this->BinaryDirectory->setCompleter(new QCMakeFileCompleter(this, true));
  134. this->SourceDirectory->setCompleter(new QCMakeFileCompleter(this, true));
  135. // fixed pitch font in output window
  136. QFont outputFont("Courier");
  137. this->Output->setFont(outputFont);
  138. this->ErrorFormat.setForeground(QBrush(Qt::red));
  139. // start the cmake worker thread
  140. this->CMakeThread = new QCMakeThread(this);
  141. QObject::connect(this->CMakeThread, SIGNAL(cmakeInitialized()),
  142. this, SLOT(initialize()), Qt::QueuedConnection);
  143. this->CMakeThread->start();
  144. this->enterState(ReadyConfigure);
  145. ProgressOffset = 0.0;
  146. ProgressFactor = 1.0;
  147. }
  148. void CMakeSetupDialog::initialize()
  149. {
  150. // now the cmake worker thread is running, lets make our connections to it
  151. QObject::connect(this->CMakeThread->cmakeInstance(),
  152. SIGNAL(propertiesChanged(const QCMakePropertyList&)),
  153. this->CacheValues->cacheModel(),
  154. SLOT(setProperties(const QCMakePropertyList&)));
  155. QObject::connect(this->ConfigureButton, SIGNAL(clicked(bool)),
  156. this, SLOT(doConfigure()));
  157. QObject::connect(this->CMakeThread->cmakeInstance(), SIGNAL(configureDone(int)),
  158. this, SLOT(exitLoop(int)));
  159. QObject::connect(this->CMakeThread->cmakeInstance(), SIGNAL(generateDone(int)),
  160. this, SLOT(exitLoop(int)));
  161. QObject::connect(this->GenerateButton, SIGNAL(clicked(bool)),
  162. this, SLOT(doGenerate()));
  163. QObject::connect(this->BrowseSourceDirectoryButton, SIGNAL(clicked(bool)),
  164. this, SLOT(doSourceBrowse()));
  165. QObject::connect(this->BrowseBinaryDirectoryButton, SIGNAL(clicked(bool)),
  166. this, SLOT(doBinaryBrowse()));
  167. QObject::connect(this->BinaryDirectory, SIGNAL(editTextChanged(QString)),
  168. this, SLOT(onBinaryDirectoryChanged(QString)));
  169. QObject::connect(this->SourceDirectory, SIGNAL(textChanged(QString)),
  170. this, SLOT(onSourceDirectoryChanged(QString)));
  171. QObject::connect(this->CMakeThread->cmakeInstance(),
  172. SIGNAL(sourceDirChanged(QString)),
  173. this, SLOT(updateSourceDirectory(QString)));
  174. QObject::connect(this->CMakeThread->cmakeInstance(),
  175. SIGNAL(binaryDirChanged(QString)),
  176. this, SLOT(updateBinaryDirectory(QString)));
  177. QObject::connect(this->CMakeThread->cmakeInstance(),
  178. SIGNAL(progressChanged(QString, float)),
  179. this, SLOT(showProgress(QString,float)));
  180. QObject::connect(this->CMakeThread->cmakeInstance(),
  181. SIGNAL(errorMessage(QString)),
  182. this, SLOT(error(QString)));
  183. QObject::connect(this->CMakeThread->cmakeInstance(),
  184. SIGNAL(outputMessage(QString)),
  185. this, SLOT(message(QString)));
  186. QObject::connect(this->groupedCheck, SIGNAL(toggled(bool)),
  187. this, SLOT(setGroupedView(bool)));
  188. QObject::connect(this->advancedCheck, SIGNAL(toggled(bool)),
  189. this, SLOT(setAdvancedView(bool)));
  190. QObject::connect(this->Search, SIGNAL(textChanged(QString)),
  191. this, SLOT(setSearchFilter(QString)));
  192. QObject::connect(this->CMakeThread->cmakeInstance(),
  193. SIGNAL(generatorChanged(QString)),
  194. this, SLOT(updateGeneratorLabel(QString)));
  195. this->updateGeneratorLabel(QString());
  196. QObject::connect(this->CacheValues->cacheModel(),
  197. SIGNAL(dataChanged(QModelIndex,QModelIndex)),
  198. this, SLOT(setCacheModified()));
  199. QObject::connect(this->CacheValues->selectionModel(),
  200. SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
  201. this, SLOT(selectionChanged()));
  202. QObject::connect(this->RemoveEntry, SIGNAL(clicked(bool)),
  203. this, SLOT(removeSelectedCacheEntries()));
  204. QObject::connect(this->AddEntry, SIGNAL(clicked(bool)),
  205. this, SLOT(addCacheEntry()));
  206. QObject::connect(this->SuppressDevWarningsAction, SIGNAL(triggered(bool)),
  207. this->CMakeThread->cmakeInstance(), SLOT(setSuppressDevWarnings(bool)));
  208. if(!this->SourceDirectory->text().isEmpty() ||
  209. !this->BinaryDirectory->lineEdit()->text().isEmpty())
  210. {
  211. this->onSourceDirectoryChanged(this->SourceDirectory->text());
  212. this->onBinaryDirectoryChanged(this->BinaryDirectory->lineEdit()->text());
  213. }
  214. else
  215. {
  216. this->onBinaryDirectoryChanged(this->BinaryDirectory->lineEdit()->text());
  217. }
  218. }
  219. CMakeSetupDialog::~CMakeSetupDialog()
  220. {
  221. QSettings settings;
  222. settings.beginGroup("Settings/StartPath");
  223. settings.setValue("Height", this->height());
  224. settings.setValue("Width", this->width());
  225. settings.setValue("SplitterSizes", this->Splitter->saveState());
  226. // wait for thread to stop
  227. this->CMakeThread->quit();
  228. this->CMakeThread->wait(2000);
  229. }
  230. bool CMakeSetupDialog::prepareConfigure()
  231. {
  232. // make sure build directory exists
  233. QString bindir = this->CMakeThread->cmakeInstance()->binaryDirectory();
  234. QDir dir(bindir);
  235. if(!dir.exists())
  236. {
  237. QString msg = tr("Build directory does not exist, "
  238. "should I create it?")
  239. + "\n\n"
  240. + tr("Directory: ");
  241. msg += bindir;
  242. QString title = tr("Create Directory");
  243. QMessageBox::StandardButton btn;
  244. btn = QMessageBox::information(this, title, msg,
  245. QMessageBox::Yes | QMessageBox::No);
  246. if(btn == QMessageBox::No)
  247. {
  248. return false;
  249. }
  250. if(!dir.mkpath("."))
  251. {
  252. QMessageBox::information(this, tr("Create Directory Failed"),
  253. QString(tr("Failed to create directory %1")).arg(dir.path()),
  254. QMessageBox::Ok);
  255. return false;
  256. }
  257. }
  258. // if no generator, prompt for it and other setup stuff
  259. if(this->CMakeThread->cmakeInstance()->generator().isEmpty())
  260. {
  261. if(!this->setupFirstConfigure())
  262. {
  263. return false;
  264. }
  265. }
  266. // remember path
  267. this->addBinaryPath(dir.absolutePath());
  268. return true;
  269. }
  270. void CMakeSetupDialog::exitLoop(int err)
  271. {
  272. this->LocalLoop.exit(err);
  273. }
  274. void CMakeSetupDialog::doConfigure()
  275. {
  276. if(this->CurrentState == Configuring)
  277. {
  278. // stop configure
  279. doInterrupt();
  280. return;
  281. }
  282. if(!prepareConfigure())
  283. {
  284. return;
  285. }
  286. this->enterState(Configuring);
  287. bool ret = doConfigureInternal();
  288. if(ret)
  289. {
  290. this->ConfigureNeeded = false;
  291. }
  292. if(ret && !this->CacheValues->cacheModel()->newPropertyCount())
  293. {
  294. this->enterState(ReadyGenerate);
  295. }
  296. else
  297. {
  298. this->enterState(ReadyConfigure);
  299. this->CacheValues->scrollToTop();
  300. }
  301. this->ProgressBar->reset();
  302. }
  303. bool CMakeSetupDialog::doConfigureInternal()
  304. {
  305. this->Output->clear();
  306. this->CacheValues->selectionModel()->clear();
  307. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  308. "setProperties", Qt::QueuedConnection,
  309. Q_ARG(QCMakePropertyList,
  310. this->CacheValues->cacheModel()->properties()));
  311. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  312. "configure", Qt::QueuedConnection);
  313. int err = this->LocalLoop.exec();
  314. if(err != 0)
  315. {
  316. QMessageBox::critical(this, tr("Error"),
  317. tr("Error in configuration process, project files may be invalid"),
  318. QMessageBox::Ok);
  319. }
  320. return 0 == err;
  321. }
  322. void CMakeSetupDialog::doInstallForCommandLine()
  323. {
  324. QMacInstallDialog setupdialog(0);
  325. setupdialog.exec();
  326. }
  327. bool CMakeSetupDialog::doGenerateInternal()
  328. {
  329. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  330. "generate", Qt::QueuedConnection);
  331. int err = this->LocalLoop.exec();
  332. if(err != 0)
  333. {
  334. QMessageBox::critical(this, tr("Error"),
  335. tr("Error in generation process, project files may be invalid"),
  336. QMessageBox::Ok);
  337. }
  338. return 0 == err;
  339. }
  340. void CMakeSetupDialog::doGenerate()
  341. {
  342. if(this->CurrentState == Generating)
  343. {
  344. // stop generate
  345. doInterrupt();
  346. return;
  347. }
  348. // see if we need to configure
  349. // we'll need to configure if:
  350. // the configure step hasn't been done yet
  351. // generate was the last step done
  352. if(this->ConfigureNeeded)
  353. {
  354. if(!prepareConfigure())
  355. {
  356. return;
  357. }
  358. }
  359. this->enterState(Generating);
  360. bool config_passed = true;
  361. if(this->ConfigureNeeded)
  362. {
  363. this->CacheValues->cacheModel()->setShowNewProperties(false);
  364. this->ProgressFactor = 0.5;
  365. config_passed = doConfigureInternal();
  366. this->ProgressOffset = 0.5;
  367. }
  368. if(config_passed)
  369. {
  370. doGenerateInternal();
  371. }
  372. this->ProgressOffset = 0.0;
  373. this->ProgressFactor = 1.0;
  374. this->CacheValues->cacheModel()->setShowNewProperties(true);
  375. this->enterState(ReadyConfigure);
  376. this->ProgressBar->reset();
  377. this->ConfigureNeeded = true;
  378. }
  379. void CMakeSetupDialog::closeEvent(QCloseEvent* e)
  380. {
  381. // prompt for close if there are unsaved changes, and we're not busy
  382. if(this->CacheModified)
  383. {
  384. QString msg = tr("You have changed options but not rebuilt, "
  385. "are you sure you want to exit?");
  386. QString title = tr("Confirm Exit");
  387. QMessageBox::StandardButton btn;
  388. btn = QMessageBox::critical(this, title, msg,
  389. QMessageBox::Yes | QMessageBox::No);
  390. if(btn == QMessageBox::No)
  391. {
  392. e->ignore();
  393. }
  394. }
  395. // don't close if we're busy, unless the user really wants to
  396. if(this->CurrentState == Configuring)
  397. {
  398. QString msg = "You are in the middle of a Configure.\n"
  399. "If you Exit now the configure information will be lost.\n"
  400. "Are you sure you want to Exit?";
  401. QString title = tr("Confirm Exit");
  402. QMessageBox::StandardButton btn;
  403. btn = QMessageBox::critical(this, title, msg,
  404. QMessageBox::Yes | QMessageBox::No);
  405. if(btn == QMessageBox::No)
  406. {
  407. e->ignore();
  408. }
  409. else
  410. {
  411. this->doInterrupt();
  412. }
  413. }
  414. // let the generate finish
  415. if(this->CurrentState == Generating)
  416. {
  417. e->ignore();
  418. }
  419. }
  420. void CMakeSetupDialog::doHelp()
  421. {
  422. QString msg = tr("CMake is used to configure and generate build files for "
  423. "software projects. The basic steps for configuring a project are as "
  424. "follows:\r\n\r\n1. Select the source directory for the project. This should "
  425. "contain the CMakeLists.txt files for the project.\r\n\r\n2. Select the build "
  426. "directory for the project. This is the directory where the project will be "
  427. "built. It can be the same or a different directory than the source "
  428. "directory. For easy clean up, a separate build directory is recommended. "
  429. "CMake will create the directory if it does not exist.\r\n\r\n3. Once the "
  430. "source and binary directories are selected, it is time to press the "
  431. "Configure button. This will cause CMake to read all of the input files and "
  432. "discover all the variables used by the project. The first time a variable "
  433. "is displayed it will be in Red. Users should inspect red variables making "
  434. "sure the values are correct. For some projects the Configure process can "
  435. "be iterative, so continue to press the Configure button until there are no "
  436. "longer red entries.\r\n\r\n4. Once there are no longer red entries, you "
  437. "should click the Generate button. This will write the build files to the build "
  438. "directory.");
  439. QDialog dialog;
  440. QFontMetrics met(this->font());
  441. int msgWidth = met.width(msg);
  442. dialog.setMinimumSize(msgWidth/15,20);
  443. dialog.setWindowTitle(tr("Help"));
  444. QVBoxLayout* l = new QVBoxLayout(&dialog);
  445. QLabel* lab = new QLabel(&dialog);
  446. lab->setText(msg);
  447. lab->setWordWrap(true);
  448. QDialogButtonBox* btns = new QDialogButtonBox(QDialogButtonBox::Ok,
  449. Qt::Horizontal, &dialog);
  450. QObject::connect(btns, SIGNAL(accepted()), &dialog, SLOT(accept()));
  451. l->addWidget(lab);
  452. l->addWidget(btns);
  453. dialog.exec();
  454. }
  455. void CMakeSetupDialog::doInterrupt()
  456. {
  457. this->enterState(Interrupting);
  458. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  459. "interrupt", Qt::QueuedConnection);
  460. }
  461. void CMakeSetupDialog::doSourceBrowse()
  462. {
  463. QString dir = QFileDialog::getExistingDirectory(this,
  464. tr("Enter Path to Source"), this->SourceDirectory->text());
  465. if(!dir.isEmpty())
  466. {
  467. this->setSourceDirectory(dir);
  468. }
  469. }
  470. void CMakeSetupDialog::updateSourceDirectory(const QString& dir)
  471. {
  472. if(this->SourceDirectory->text() != dir)
  473. {
  474. this->SourceDirectory->blockSignals(true);
  475. this->SourceDirectory->setText(dir);
  476. this->SourceDirectory->blockSignals(false);
  477. }
  478. }
  479. void CMakeSetupDialog::updateBinaryDirectory(const QString& dir)
  480. {
  481. if(this->BinaryDirectory->currentText() != dir)
  482. {
  483. this->BinaryDirectory->blockSignals(true);
  484. this->BinaryDirectory->setEditText(dir);
  485. this->BinaryDirectory->blockSignals(false);
  486. }
  487. }
  488. void CMakeSetupDialog::doBinaryBrowse()
  489. {
  490. QString dir = QFileDialog::getExistingDirectory(this,
  491. tr("Enter Path to Build"), this->BinaryDirectory->currentText());
  492. if(!dir.isEmpty() && dir != this->BinaryDirectory->currentText())
  493. {
  494. this->setBinaryDirectory(dir);
  495. }
  496. }
  497. void CMakeSetupDialog::setBinaryDirectory(const QString& dir)
  498. {
  499. this->BinaryDirectory->setEditText(dir);
  500. }
  501. void CMakeSetupDialog::onSourceDirectoryChanged(const QString& dir)
  502. {
  503. this->Output->clear();
  504. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  505. "setSourceDirectory", Qt::QueuedConnection, Q_ARG(QString, dir));
  506. }
  507. void CMakeSetupDialog::onBinaryDirectoryChanged(const QString& dir)
  508. {
  509. QString title = QString(tr("CMake %1 - %2"));
  510. title = title.arg(cmVersion::GetCMakeVersion());
  511. title = title.arg(dir);
  512. this->setWindowTitle(title);
  513. this->CacheModified = false;
  514. this->CacheValues->cacheModel()->clear();
  515. qobject_cast<QCMakeCacheModelDelegate*>(this->CacheValues->itemDelegate())->clearChanges();
  516. this->Output->clear();
  517. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  518. "setBinaryDirectory", Qt::QueuedConnection, Q_ARG(QString, dir));
  519. }
  520. void CMakeSetupDialog::setSourceDirectory(const QString& dir)
  521. {
  522. this->SourceDirectory->setText(dir);
  523. }
  524. void CMakeSetupDialog::showProgress(const QString& /*msg*/, float percent)
  525. {
  526. percent = (percent * ProgressFactor) + ProgressOffset;
  527. this->ProgressBar->setValue(qRound(percent * 100));
  528. }
  529. void CMakeSetupDialog::error(const QString& msg)
  530. {
  531. this->Output->setCurrentCharFormat(this->ErrorFormat);
  532. this->Output->append(msg);
  533. }
  534. void CMakeSetupDialog::message(const QString& msg)
  535. {
  536. this->Output->setCurrentCharFormat(this->MessageFormat);
  537. this->Output->append(msg);
  538. }
  539. void CMakeSetupDialog::setEnabledState(bool enabled)
  540. {
  541. // disable parts of the GUI during configure/generate
  542. this->CacheValues->cacheModel()->setEditEnabled(enabled);
  543. this->SourceDirectory->setEnabled(enabled);
  544. this->BrowseSourceDirectoryButton->setEnabled(enabled);
  545. this->BinaryDirectory->setEnabled(enabled);
  546. this->BrowseBinaryDirectoryButton->setEnabled(enabled);
  547. this->ReloadCacheAction->setEnabled(enabled);
  548. this->DeleteCacheAction->setEnabled(enabled);
  549. this->ExitAction->setEnabled(enabled);
  550. this->ConfigureAction->setEnabled(enabled);
  551. this->AddEntry->setEnabled(enabled);
  552. this->RemoveEntry->setEnabled(false); // let selection re-enable it
  553. }
  554. bool CMakeSetupDialog::setupFirstConfigure()
  555. {
  556. FirstConfigure dialog;
  557. // initialize dialog and restore saved settings
  558. // add generators
  559. dialog.setGenerators(this->CMakeThread->cmakeInstance()->availableGenerators());
  560. // restore from settings
  561. dialog.loadFromSettings();
  562. if(dialog.exec() == QDialog::Accepted)
  563. {
  564. dialog.saveToSettings();
  565. this->CMakeThread->cmakeInstance()->setGenerator(dialog.getGenerator());
  566. QCMakeCacheModel* m = this->CacheValues->cacheModel();
  567. if(dialog.compilerSetup())
  568. {
  569. QString fortranCompiler = dialog.getFortranCompiler();
  570. if(!fortranCompiler.isEmpty())
  571. {
  572. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_Fortran_COMPILER",
  573. "Fortran compiler.", fortranCompiler, false);
  574. }
  575. QString cxxCompiler = dialog.getCXXCompiler();
  576. if(!cxxCompiler.isEmpty())
  577. {
  578. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_CXX_COMPILER",
  579. "CXX compiler.", cxxCompiler, false);
  580. }
  581. QString cCompiler = dialog.getCCompiler();
  582. if(!cCompiler.isEmpty())
  583. {
  584. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_C_COMPILER",
  585. "C compiler.", cCompiler, false);
  586. }
  587. }
  588. else if(dialog.crossCompilerSetup())
  589. {
  590. QString fortranCompiler = dialog.getFortranCompiler();
  591. if(!fortranCompiler.isEmpty())
  592. {
  593. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_Fortran_COMPILER",
  594. "Fortran compiler.", fortranCompiler, false);
  595. }
  596. QString mode = dialog.getCrossIncludeMode();
  597. m->insertProperty(QCMakeProperty::STRING, "CMAKE_FIND_ROOT_PATH_MODE_INCLUDE",
  598. "CMake Find Include Mode", mode, false);
  599. mode = dialog.getCrossLibraryMode();
  600. m->insertProperty(QCMakeProperty::STRING, "CMAKE_FIND_ROOT_PATH_MODE_LIBRARY",
  601. "CMake Find Library Mode", mode, false);
  602. mode = dialog.getCrossProgramMode();
  603. m->insertProperty(QCMakeProperty::STRING, "CMAKE_FIND_ROOT_PATH_MODE_PROGRAM",
  604. "CMake Find Program Mode", mode, false);
  605. QString rootPath = dialog.getCrossRoot();
  606. m->insertProperty(QCMakeProperty::PATH, "CMAKE_FIND_ROOT_PATH",
  607. "CMake Find Root Path", rootPath, false);
  608. QString systemName = dialog.getSystemName();
  609. m->insertProperty(QCMakeProperty::STRING, "CMAKE_SYSTEM_NAME",
  610. "CMake System Name", systemName, false);
  611. QString cxxCompiler = dialog.getCXXCompiler();
  612. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_CXX_COMPILER",
  613. "CXX compiler.", cxxCompiler, false);
  614. QString cCompiler = dialog.getCCompiler();
  615. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_C_COMPILER",
  616. "C compiler.", cCompiler, false);
  617. }
  618. else if(dialog.crossCompilerToolChainFile())
  619. {
  620. QString toolchainFile = dialog.getCrossCompilerToolChainFile();
  621. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_TOOLCHAIN_FILE",
  622. "Cross Compile ToolChain File", toolchainFile, false);
  623. }
  624. return true;
  625. }
  626. return false;
  627. }
  628. void CMakeSetupDialog::updateGeneratorLabel(const QString& gen)
  629. {
  630. QString str = tr("Current Generator: ");
  631. if(gen.isEmpty())
  632. {
  633. str += tr("None");
  634. }
  635. else
  636. {
  637. str += gen;
  638. }
  639. this->Generator->setText(str);
  640. }
  641. void CMakeSetupDialog::doReloadCache()
  642. {
  643. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  644. "reloadCache", Qt::QueuedConnection);
  645. }
  646. void CMakeSetupDialog::doDeleteCache()
  647. {
  648. QString title = tr("Delete Cache");
  649. QString msg = "Are you sure you want to delete the cache?";
  650. QMessageBox::StandardButton btn;
  651. btn = QMessageBox::information(this, title, msg,
  652. QMessageBox::Yes | QMessageBox::No);
  653. if(btn == QMessageBox::No)
  654. {
  655. return;
  656. }
  657. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  658. "deleteCache", Qt::QueuedConnection);
  659. }
  660. void CMakeSetupDialog::doAbout()
  661. {
  662. QString msg = "CMake %1\n"
  663. "Using Qt %2\n"
  664. "www.cmake.org";
  665. msg = msg.arg(cmVersion::GetCMakeVersion());
  666. msg = msg.arg(qVersion());
  667. QDialog dialog;
  668. dialog.setWindowTitle(tr("About"));
  669. QVBoxLayout* l = new QVBoxLayout(&dialog);
  670. QLabel* lab = new QLabel(&dialog);
  671. l->addWidget(lab);
  672. lab->setText(msg);
  673. lab->setWordWrap(true);
  674. QDialogButtonBox* btns = new QDialogButtonBox(QDialogButtonBox::Ok,
  675. Qt::Horizontal, &dialog);
  676. QObject::connect(btns, SIGNAL(accepted()), &dialog, SLOT(accept()));
  677. l->addWidget(btns);
  678. dialog.exec();
  679. }
  680. void CMakeSetupDialog::setExitAfterGenerate(bool b)
  681. {
  682. this->ExitAfterGenerate = b;
  683. }
  684. void CMakeSetupDialog::addBinaryPath(const QString& path)
  685. {
  686. QString cleanpath = QDir::cleanPath(path);
  687. // update UI
  688. this->BinaryDirectory->blockSignals(true);
  689. int idx = this->BinaryDirectory->findText(cleanpath);
  690. if(idx != -1)
  691. {
  692. this->BinaryDirectory->removeItem(idx);
  693. }
  694. this->BinaryDirectory->insertItem(0, cleanpath);
  695. this->BinaryDirectory->setCurrentIndex(0);
  696. this->BinaryDirectory->blockSignals(false);
  697. // save to registry
  698. QStringList buildPaths = this->loadBuildPaths();
  699. buildPaths.removeAll(cleanpath);
  700. buildPaths.prepend(cleanpath);
  701. this->saveBuildPaths(buildPaths);
  702. }
  703. void CMakeSetupDialog::dragEnterEvent(QDragEnterEvent* e)
  704. {
  705. if(!(this->CurrentState == ReadyConfigure ||
  706. this->CurrentState == ReadyGenerate))
  707. {
  708. e->ignore();
  709. return;
  710. }
  711. const QMimeData* dat = e->mimeData();
  712. QList<QUrl> urls = dat->urls();
  713. QString file = urls.count() ? urls[0].toLocalFile() : QString();
  714. if(!file.isEmpty() &&
  715. (file.endsWith("CMakeCache.txt", Qt::CaseInsensitive) ||
  716. file.endsWith("CMakeLists.txt", Qt::CaseInsensitive) ) )
  717. {
  718. e->accept();
  719. }
  720. else
  721. {
  722. e->ignore();
  723. }
  724. }
  725. void CMakeSetupDialog::dropEvent(QDropEvent* e)
  726. {
  727. if(!(this->CurrentState == ReadyConfigure ||
  728. this->CurrentState == ReadyGenerate))
  729. {
  730. return;
  731. }
  732. const QMimeData* dat = e->mimeData();
  733. QList<QUrl> urls = dat->urls();
  734. QString file = urls.count() ? urls[0].toLocalFile() : QString();
  735. if(file.endsWith("CMakeCache.txt", Qt::CaseInsensitive))
  736. {
  737. QFileInfo info(file);
  738. if(this->CMakeThread->cmakeInstance()->binaryDirectory() != info.absolutePath())
  739. {
  740. this->setBinaryDirectory(info.absolutePath());
  741. }
  742. }
  743. else if(file.endsWith("CMakeLists.txt", Qt::CaseInsensitive))
  744. {
  745. QFileInfo info(file);
  746. if(this->CMakeThread->cmakeInstance()->binaryDirectory() != info.absolutePath())
  747. {
  748. this->setSourceDirectory(info.absolutePath());
  749. this->setBinaryDirectory(info.absolutePath());
  750. }
  751. }
  752. }
  753. QStringList CMakeSetupDialog::loadBuildPaths()
  754. {
  755. QSettings settings;
  756. settings.beginGroup("Settings/StartPath");
  757. QStringList buildPaths;
  758. for(int i=0; i<10; i++)
  759. {
  760. QString p = settings.value(QString("WhereBuild%1").arg(i)).toString();
  761. if(!p.isEmpty())
  762. {
  763. buildPaths.append(p);
  764. }
  765. }
  766. return buildPaths;
  767. }
  768. void CMakeSetupDialog::saveBuildPaths(const QStringList& paths)
  769. {
  770. QSettings settings;
  771. settings.beginGroup("Settings/StartPath");
  772. int num = paths.count();
  773. if(num > 10)
  774. {
  775. num = 10;
  776. }
  777. for(int i=0; i<num; i++)
  778. {
  779. settings.setValue(QString("WhereBuild%1").arg(i), paths[i]);
  780. }
  781. }
  782. void CMakeSetupDialog::setCacheModified()
  783. {
  784. this->CacheModified = true;
  785. this->enterState(ReadyConfigure);
  786. }
  787. void CMakeSetupDialog::removeSelectedCacheEntries()
  788. {
  789. QModelIndexList idxs = this->CacheValues->selectionModel()->selectedRows();
  790. QList<QPersistentModelIndex> pidxs;
  791. foreach(QModelIndex i, idxs)
  792. {
  793. pidxs.append(i);
  794. }
  795. foreach(QPersistentModelIndex pi, pidxs)
  796. {
  797. this->CacheValues->model()->removeRow(pi.row(), pi.parent());
  798. }
  799. }
  800. void CMakeSetupDialog::selectionChanged()
  801. {
  802. QModelIndexList idxs = this->CacheValues->selectionModel()->selectedRows();
  803. if(idxs.count() &&
  804. (this->CurrentState == ReadyConfigure ||
  805. this->CurrentState == ReadyGenerate) )
  806. {
  807. this->RemoveEntry->setEnabled(true);
  808. }
  809. else
  810. {
  811. this->RemoveEntry->setEnabled(false);
  812. }
  813. }
  814. void CMakeSetupDialog::enterState(CMakeSetupDialog::State s)
  815. {
  816. if(s == this->CurrentState)
  817. {
  818. return;
  819. }
  820. this->CurrentState = s;
  821. if(s == Interrupting)
  822. {
  823. this->ConfigureButton->setEnabled(false);
  824. this->GenerateButton->setEnabled(false);
  825. }
  826. else if(s == Configuring)
  827. {
  828. this->setEnabledState(false);
  829. this->GenerateButton->setEnabled(false);
  830. this->GenerateAction->setEnabled(false);
  831. this->ConfigureButton->setText(tr("&Stop"));
  832. }
  833. else if(s == Generating)
  834. {
  835. this->CacheModified = false;
  836. this->setEnabledState(false);
  837. this->ConfigureButton->setEnabled(false);
  838. this->GenerateAction->setEnabled(false);
  839. this->GenerateButton->setText(tr("&Stop"));
  840. }
  841. else if(s == ReadyConfigure)
  842. {
  843. this->setEnabledState(true);
  844. this->GenerateButton->setEnabled(true);
  845. this->GenerateAction->setEnabled(true);
  846. this->ConfigureButton->setEnabled(true);
  847. this->ConfigureButton->setText(tr("&Configure"));
  848. this->GenerateButton->setText(tr("&Generate"));
  849. }
  850. else if(s == ReadyGenerate)
  851. {
  852. this->setEnabledState(true);
  853. this->GenerateButton->setEnabled(true);
  854. this->GenerateAction->setEnabled(true);
  855. this->ConfigureButton->setEnabled(true);
  856. this->ConfigureButton->setText(tr("&Configure"));
  857. this->GenerateButton->setText(tr("&Generate"));
  858. }
  859. }
  860. void CMakeSetupDialog::addCacheEntry()
  861. {
  862. QDialog dialog(this);
  863. dialog.resize(400, 200);
  864. dialog.setWindowTitle(tr("Add Cache Entry"));
  865. QVBoxLayout* l = new QVBoxLayout(&dialog);
  866. AddCacheEntry* w = new AddCacheEntry(&dialog);
  867. QDialogButtonBox* btns = new QDialogButtonBox(
  868. QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
  869. Qt::Horizontal, &dialog);
  870. QObject::connect(btns, SIGNAL(accepted()), &dialog, SLOT(accept()));
  871. QObject::connect(btns, SIGNAL(rejected()), &dialog, SLOT(reject()));
  872. l->addWidget(w);
  873. l->addStretch();
  874. l->addWidget(btns);
  875. if(QDialog::Accepted == dialog.exec())
  876. {
  877. QCMakeCacheModel* m = this->CacheValues->cacheModel();
  878. m->insertProperty(w->type(), w->name(), w->description(), w->value(), false);
  879. }
  880. }
  881. void CMakeSetupDialog::startSearch()
  882. {
  883. this->Search->setFocus(Qt::OtherFocusReason);
  884. this->Search->selectAll();
  885. }
  886. void CMakeSetupDialog::setDebugOutput(bool flag)
  887. {
  888. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  889. "setDebugOutput", Qt::QueuedConnection, Q_ARG(bool, flag));
  890. }
  891. void CMakeSetupDialog::setGroupedView(bool v)
  892. {
  893. this->CacheValues->cacheModel()->setViewType(v ? QCMakeCacheModel::GroupView : QCMakeCacheModel::FlatView);
  894. this->CacheValues->setRootIsDecorated(v);
  895. QSettings settings;
  896. settings.beginGroup("Settings/StartPath");
  897. settings.setValue("GroupView", v);
  898. }
  899. void CMakeSetupDialog::setAdvancedView(bool v)
  900. {
  901. this->CacheValues->setShowAdvanced(v);
  902. QSettings settings;
  903. settings.beginGroup("Settings/StartPath");
  904. settings.setValue("AdvancedView", v);
  905. }
  906. void CMakeSetupDialog::showUserChanges()
  907. {
  908. QSet<QCMakeProperty> changes =
  909. qobject_cast<QCMakeCacheModelDelegate*>(this->CacheValues->itemDelegate())->changes();
  910. QDialog dialog(this);
  911. dialog.setWindowTitle(tr("My Changes"));
  912. dialog.resize(600, 400);
  913. QVBoxLayout* l = new QVBoxLayout(&dialog);
  914. QTextEdit* textedit = new QTextEdit(&dialog);
  915. textedit->setReadOnly(true);
  916. l->addWidget(textedit);
  917. QDialogButtonBox* btns = new QDialogButtonBox(QDialogButtonBox::Close,
  918. Qt::Horizontal, &dialog);
  919. QObject::connect(btns, SIGNAL(rejected()), &dialog, SLOT(accept()));
  920. l->addWidget(btns);
  921. QString command;
  922. QString cache;
  923. foreach(QCMakeProperty prop, changes)
  924. {
  925. QString type;
  926. switch(prop.Type)
  927. {
  928. case QCMakeProperty::BOOL:
  929. type = "BOOL";
  930. break;
  931. case QCMakeProperty::PATH:
  932. type = "PATH";
  933. break;
  934. case QCMakeProperty::FILEPATH:
  935. type = "FILEPATH";
  936. break;
  937. case QCMakeProperty::STRING:
  938. type = "STRING";
  939. break;
  940. }
  941. QString value;
  942. if(prop.Type == QCMakeProperty::BOOL)
  943. {
  944. value = prop.Value.toBool() ? "1" : "0";
  945. }
  946. else
  947. {
  948. value = prop.Value.toString();
  949. }
  950. QString line("%1:%2=");
  951. line = line.arg(prop.Key);
  952. line = line.arg(type);
  953. command += QString("-D%1\"%2\" ").arg(line).arg(value);
  954. cache += QString("%1%2\n").arg(line).arg(value);
  955. }
  956. textedit->append(tr("Commandline options:"));
  957. textedit->append(command);
  958. textedit->append("\n");
  959. textedit->append(tr("Cache file:"));
  960. textedit->append(cache);
  961. dialog.exec();
  962. }
  963. void CMakeSetupDialog::setSearchFilter(const QString& str)
  964. {
  965. this->CacheValues->selectionModel()->clear();
  966. this->CacheValues->setSearchFilter(str);
  967. }