CMakeSetupDialog.cxx 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "CMakeSetupDialog.h"
  4. #include <cm/memory>
  5. #include <QCloseEvent>
  6. #include <QCoreApplication>
  7. #include <QDesktopServices>
  8. #include <QDialogButtonBox>
  9. #include <QDragEnterEvent>
  10. #include <QFileDialog>
  11. #include <QInputDialog>
  12. #include <QKeySequence>
  13. #include <QMenu>
  14. #include <QMenuBar>
  15. #include <QMessageBox>
  16. #include <QMimeData>
  17. #include <QProcessEnvironment>
  18. #include <QProgressBar>
  19. #include <QSettings>
  20. #include <QShortcut>
  21. #include <QStatusBar>
  22. #include <QString>
  23. #include <QToolButton>
  24. #include <QUrl>
  25. #include <QVector>
  26. #ifdef QT_WINEXTRAS
  27. # include <QWinTaskbarButton>
  28. # include <QWinTaskbarProgress>
  29. #endif
  30. #include "QCMake.h"
  31. #include "QCMakeCacheView.h"
  32. #include "cmSystemTools.h"
  33. #include "cmVersion.h"
  34. #include "AddCacheEntry.h"
  35. #include "EnvironmentDialog.h"
  36. #include "FirstConfigure.h"
  37. #include "RegexExplorer.h"
  38. #include "WarningMessagesDialog.h"
  39. namespace {
  40. const QString PRESETS_DISABLED_TOOLTIP =
  41. "This option is disabled because there are no available presets in "
  42. "CMakePresets.json or CMakeUserPresets.json.";
  43. }
  44. QCMakeThread::QCMakeThread(QObject* p)
  45. : QThread(p)
  46. {
  47. }
  48. QCMake* QCMakeThread::cmakeInstance() const
  49. {
  50. return this->CMakeInstance.get();
  51. }
  52. void QCMakeThread::run()
  53. {
  54. this->CMakeInstance = cm::make_unique<QCMake>();
  55. // emit that this cmake thread is ready for use
  56. emit this->cmakeInitialized();
  57. this->exec();
  58. this->CMakeInstance.reset();
  59. }
  60. CMakeSetupDialog::CMakeSetupDialog()
  61. : ExitAfterGenerate(true)
  62. , CacheModified(false)
  63. , ConfigureNeeded(true)
  64. , CurrentState(Interrupting)
  65. {
  66. QString title = QString(tr("CMake %1"));
  67. title = title.arg(cmVersion::GetCMakeVersion());
  68. this->setWindowTitle(title);
  69. // create the GUI
  70. QSettings settings;
  71. settings.beginGroup("Settings/StartPath");
  72. restoreGeometry(settings.value("geometry").toByteArray());
  73. restoreState(settings.value("windowState").toByteArray());
  74. this->AddVariableNames =
  75. settings.value("AddVariableNames", QStringList("CMAKE_INSTALL_PREFIX"))
  76. .toStringList();
  77. this->AddVariableTypes =
  78. settings.value("AddVariableTypes", QStringList("PATH")).toStringList();
  79. QWidget* cont = new QWidget(this);
  80. this->setupUi(cont);
  81. this->Splitter->setStretchFactor(0, 3);
  82. this->Splitter->setStretchFactor(1, 1);
  83. this->setCentralWidget(cont);
  84. this->ProgressBar->reset();
  85. this->RemoveEntry->setEnabled(false);
  86. this->AddEntry->setEnabled(false);
  87. this->Preset->setStatusTip(PRESETS_DISABLED_TOOLTIP);
  88. QByteArray p = settings.value("SplitterSizes").toByteArray();
  89. this->Splitter->restoreState(p);
  90. bool groupView = settings.value("GroupView", false).toBool();
  91. this->setGroupedView(groupView);
  92. this->groupedCheck->setCheckState(groupView ? Qt::Checked : Qt::Unchecked);
  93. bool advancedView = settings.value("AdvancedView", false).toBool();
  94. this->setAdvancedView(advancedView);
  95. this->advancedCheck->setCheckState(advancedView ? Qt::Checked
  96. : Qt::Unchecked);
  97. QMenu* FileMenu = this->menuBar()->addMenu(tr("&File"));
  98. this->ReloadCacheAction = FileMenu->addAction(tr("&Reload Cache"));
  99. QObject::connect(this->ReloadCacheAction, &QAction::triggered, this,
  100. &CMakeSetupDialog::doReloadCache);
  101. this->DeleteCacheAction = FileMenu->addAction(tr("&Delete Cache"));
  102. QObject::connect(this->DeleteCacheAction, &QAction::triggered, this,
  103. &CMakeSetupDialog::doDeleteCache);
  104. this->ExitAction = FileMenu->addAction(tr("E&xit"));
  105. QObject::connect(this->ExitAction, &QAction::triggered, this,
  106. &CMakeSetupDialog::close);
  107. this->ExitAction->setShortcut(QKeySequence::Quit);
  108. QMenu* ToolsMenu = this->menuBar()->addMenu(tr("&Tools"));
  109. this->ConfigureAction = ToolsMenu->addAction(tr("&Configure"));
  110. QObject::connect(this->ConfigureAction, &QAction::triggered, this,
  111. &CMakeSetupDialog::doConfigure);
  112. // prevent merging with Preferences menu item on macOS
  113. this->ConfigureAction->setMenuRole(QAction::NoRole);
  114. this->GenerateAction = ToolsMenu->addAction(tr("&Generate"));
  115. QObject::connect(this->GenerateAction, &QAction::triggered, this,
  116. &CMakeSetupDialog::doGenerate);
  117. auto* a = ToolsMenu->addAction(tr("&Show My Changes"));
  118. QObject::connect(a, &QAction::triggered, this,
  119. &CMakeSetupDialog::showUserChanges);
  120. #if defined(Q_WS_MAC) || defined(Q_OS_MAC)
  121. this->InstallForCommandLineAction =
  122. ToolsMenu->addAction(tr("&How to Install For Command Line Use"));
  123. QObject::connect(this->InstallForCommandLineAction, &QAction::triggered,
  124. this, &CMakeSetupDialog::doInstallForCommandLine);
  125. #endif
  126. ToolsMenu->addSeparator();
  127. a = ToolsMenu->addAction(tr("Regular Expression Explorer..."));
  128. QObject::connect(a, &QAction::triggered, this,
  129. &CMakeSetupDialog::doRegexExplorerDialog);
  130. ToolsMenu->addSeparator();
  131. a = ToolsMenu->addAction(tr("&Find in Output..."));
  132. QObject::connect(a, &QAction::triggered, this,
  133. &CMakeSetupDialog::doOutputFindDialog);
  134. a->setShortcut(QKeySequence::Find);
  135. a = ToolsMenu->addAction(tr("Find Next"));
  136. QObject::connect(a, &QAction::triggered, this,
  137. &CMakeSetupDialog::doOutputFindNext);
  138. a->setShortcut(QKeySequence::FindNext);
  139. a = ToolsMenu->addAction(tr("Find Previous"));
  140. QObject::connect(a, &QAction::triggered, this,
  141. &CMakeSetupDialog::doOutputFindPrev);
  142. a->setShortcut(QKeySequence::FindPrevious);
  143. a = ToolsMenu->addAction(tr("Goto Next Error")); // in Visual Studio
  144. QObject::connect(a, &QAction::triggered, this,
  145. &CMakeSetupDialog::doOutputErrorNext);
  146. a->setShortcut(QKeySequence(Qt::Key_F8));
  147. auto* s = new QShortcut(this);
  148. s->setKey(QKeySequence(Qt::CTRL + Qt::Key_Period));
  149. QObject::connect(s, &QShortcut::activated, this,
  150. &CMakeSetupDialog::doOutputErrorNext); // in Eclipse
  151. QMenu* OptionsMenu = this->menuBar()->addMenu(tr("&Options"));
  152. a = OptionsMenu->addAction(tr("Warning Messages..."));
  153. QObject::connect(a, &QAction::triggered, this,
  154. &CMakeSetupDialog::doWarningMessagesDialog);
  155. this->WarnUninitializedAction =
  156. OptionsMenu->addAction(tr("&Warn Uninitialized (--warn-uninitialized)"));
  157. this->WarnUninitializedAction->setCheckable(true);
  158. QAction* debugAction = OptionsMenu->addAction(tr("&Debug Output"));
  159. debugAction->setCheckable(true);
  160. QObject::connect(debugAction, &QAction::toggled, this,
  161. &CMakeSetupDialog::setDebugOutput);
  162. OptionsMenu->addSeparator();
  163. a = OptionsMenu->addAction(tr("&Expand Grouped Entries"));
  164. QObject::connect(a, &QAction::triggered, this->CacheValues,
  165. &QCMakeCacheView::expandAll);
  166. a = OptionsMenu->addAction(tr("&Collapse Grouped Entries"));
  167. QObject::connect(a, &QAction::triggered, this->CacheValues,
  168. &QCMakeCacheView::collapseAll);
  169. QMenu* HelpMenu = this->menuBar()->addMenu(tr("&Help"));
  170. a = HelpMenu->addAction(tr("Help"));
  171. QObject::connect(a, &QAction::triggered, this, &CMakeSetupDialog::doHelp);
  172. a->setShortcut(QKeySequence::HelpContents);
  173. a = HelpMenu->addAction(tr("CMake Reference Manual"));
  174. QObject::connect(a, &QAction::triggered, this, []() {
  175. QString urlFormat("https://cmake.org/cmake/help/v%1.%2/");
  176. QUrl url(urlFormat.arg(QString::number(cmVersion::GetMajorVersion()),
  177. QString::number(cmVersion::GetMinorVersion())));
  178. if (!cmSystemTools::GetHTMLDoc().empty()) {
  179. url = QUrl::fromLocalFile(
  180. QDir(QString::fromLocal8Bit(cmSystemTools::GetHTMLDoc().data()))
  181. .filePath("index.html"));
  182. }
  183. QDesktopServices::openUrl(url);
  184. });
  185. a = HelpMenu->addAction(tr("About"));
  186. QObject::connect(a, &QAction::triggered, this, &CMakeSetupDialog::doAbout);
  187. this->setAcceptDrops(true);
  188. // get the saved binary directories
  189. QStringList buildPaths = this->loadBuildPaths();
  190. this->BinaryDirectory->addItems(buildPaths);
  191. this->BinaryDirectory->setCompleter(new QCMakeFileCompleter(this, true));
  192. this->SourceDirectory->setCompleter(new QCMakeFileCompleter(this, true));
  193. // fixed pitch font in output window
  194. QFont outputFont("Courier");
  195. this->Output->setFont(outputFont);
  196. this->ErrorFormat.setForeground(QBrush(Qt::red));
  197. this->Output->setContextMenuPolicy(Qt::CustomContextMenu);
  198. connect(this->Output, &QTextEdit::customContextMenuRequested, this,
  199. &CMakeSetupDialog::doOutputContextMenu);
  200. // disable open project button
  201. this->OpenProjectButton->setDisabled(true);
  202. // start the cmake worker thread
  203. this->CMakeThread = new QCMakeThread(this);
  204. QObject::connect(this->CMakeThread, &QCMakeThread::cmakeInitialized, this,
  205. &CMakeSetupDialog::initialize, Qt::QueuedConnection);
  206. this->CMakeThread->start();
  207. this->enterState(ReadyConfigure);
  208. ProgressOffset = 0.0;
  209. ProgressFactor = 1.0;
  210. }
  211. void CMakeSetupDialog::initialize()
  212. {
  213. // now the cmake worker thread is running, lets make our connections to it
  214. QObject::connect(this->CMakeThread->cmakeInstance(),
  215. &QCMake::propertiesChanged, this->CacheValues->cacheModel(),
  216. &QCMakeCacheModel::setProperties);
  217. QObject::connect(this->ConfigureButton, &QPushButton::clicked, this,
  218. &CMakeSetupDialog::doConfigure);
  219. QObject::connect(this->CMakeThread->cmakeInstance(), &QCMake::configureDone,
  220. this, &CMakeSetupDialog::exitLoop);
  221. QObject::connect(this->CMakeThread->cmakeInstance(), &QCMake::generateDone,
  222. this, &CMakeSetupDialog::exitLoop);
  223. QObject::connect(this->GenerateButton, &QPushButton::clicked, this,
  224. &CMakeSetupDialog::doGenerate);
  225. QObject::connect(this->OpenProjectButton, &QPushButton::clicked, this,
  226. &CMakeSetupDialog::doOpenProject);
  227. QObject::connect(this->BrowseSourceDirectoryButton, &QPushButton::clicked,
  228. this, &CMakeSetupDialog::doSourceBrowse);
  229. QObject::connect(this->BrowseBinaryDirectoryButton, &QPushButton::clicked,
  230. this, &CMakeSetupDialog::doBinaryBrowse);
  231. QObject::connect(this->BinaryDirectory, &QComboBox::editTextChanged, this,
  232. &CMakeSetupDialog::onBinaryDirectoryChanged);
  233. QObject::connect(this->SourceDirectory, &QLineEdit::textChanged, this,
  234. &CMakeSetupDialog::onSourceDirectoryChanged);
  235. QObject::connect(this->Preset, &QCMakePresetComboBox::presetChanged, this,
  236. &CMakeSetupDialog::onBuildPresetChanged);
  237. QObject::connect(this->CMakeThread->cmakeInstance(),
  238. &QCMake::sourceDirChanged, this,
  239. &CMakeSetupDialog::updateSourceDirectory);
  240. QObject::connect(this->CMakeThread->cmakeInstance(),
  241. &QCMake::binaryDirChanged, this,
  242. &CMakeSetupDialog::updateBinaryDirectory);
  243. QObject::connect(this->CMakeThread->cmakeInstance(), &QCMake::presetsChanged,
  244. this, &CMakeSetupDialog::updatePresets);
  245. QObject::connect(this->CMakeThread->cmakeInstance(), &QCMake::presetChanged,
  246. this, &CMakeSetupDialog::updatePreset);
  247. QObject::connect(this->CMakeThread->cmakeInstance(),
  248. &QCMake::presetLoadError, this,
  249. &CMakeSetupDialog::showPresetLoadError);
  250. QObject::connect(this->CMakeThread->cmakeInstance(),
  251. &QCMake::progressChanged, this,
  252. &CMakeSetupDialog::showProgress);
  253. QObject::connect(this->CMakeThread->cmakeInstance(), &QCMake::errorMessage,
  254. this, &CMakeSetupDialog::error);
  255. QObject::connect(this->CMakeThread->cmakeInstance(), &QCMake::outputMessage,
  256. this, &CMakeSetupDialog::message);
  257. QObject::connect(this->CMakeThread->cmakeInstance(), &QCMake::openPossible,
  258. this->OpenProjectButton, &CMakeSetupDialog::setEnabled);
  259. QObject::connect(this->groupedCheck, &QCheckBox::toggled, this,
  260. &CMakeSetupDialog::setGroupedView);
  261. QObject::connect(this->advancedCheck, &QCheckBox::toggled, this,
  262. &CMakeSetupDialog::setAdvancedView);
  263. QObject::connect(this->Search, &QLineEdit::textChanged, this,
  264. &CMakeSetupDialog::setSearchFilter);
  265. QObject::connect(this->CMakeThread->cmakeInstance(),
  266. &QCMake::generatorChanged, this,
  267. &CMakeSetupDialog::updateGeneratorLabel);
  268. this->updateGeneratorLabel(QString());
  269. QObject::connect(this->CacheValues->cacheModel(),
  270. &QCMakeCacheModel::dataChanged, this,
  271. &CMakeSetupDialog::setCacheModified);
  272. QObject::connect(this->CacheValues->selectionModel(),
  273. &QItemSelectionModel::selectionChanged, this,
  274. &CMakeSetupDialog::selectionChanged);
  275. QObject::connect(this->RemoveEntry, &QToolButton::clicked, this,
  276. &CMakeSetupDialog::removeSelectedCacheEntries);
  277. QObject::connect(this->AddEntry, &QToolButton::clicked, this,
  278. &CMakeSetupDialog::addCacheEntry);
  279. QObject::connect(this->Environment, &QToolButton::clicked, this,
  280. &CMakeSetupDialog::editEnvironment);
  281. QObject::connect(this->WarnUninitializedAction, &QAction::triggered,
  282. this->CMakeThread->cmakeInstance(),
  283. &QCMake::setWarnUninitializedMode);
  284. QObject::connect(this->CMakeThread->cmakeInstance(),
  285. &QCMake::warnUninitializedModeChanged,
  286. this->WarnUninitializedAction, &QAction::setChecked);
  287. if (!this->SourceDirectory->text().isEmpty() &&
  288. !this->DeferredPreset.isNull()) {
  289. this->onSourceDirectoryChanged(this->SourceDirectory->text());
  290. } else if (!this->SourceDirectory->text().isEmpty() ||
  291. !this->BinaryDirectory->lineEdit()->text().isEmpty()) {
  292. this->onSourceDirectoryChanged(this->SourceDirectory->text());
  293. this->onBinaryDirectoryChanged(this->BinaryDirectory->lineEdit()->text());
  294. } else {
  295. this->onBinaryDirectoryChanged(this->BinaryDirectory->lineEdit()->text());
  296. }
  297. #ifdef QT_WINEXTRAS
  298. this->TaskbarButton = new QWinTaskbarButton(this);
  299. this->TaskbarButton->setWindow(this->windowHandle());
  300. #endif
  301. }
  302. CMakeSetupDialog::~CMakeSetupDialog()
  303. {
  304. QSettings settings;
  305. settings.beginGroup("Settings/StartPath");
  306. settings.setValue("windowState", QVariant(saveState()));
  307. settings.setValue("geometry", QVariant(saveGeometry()));
  308. settings.setValue("SplitterSizes", this->Splitter->saveState());
  309. // wait for thread to stop
  310. this->CMakeThread->quit();
  311. this->CMakeThread->wait();
  312. }
  313. bool CMakeSetupDialog::prepareConfigure()
  314. {
  315. // make sure build directory exists
  316. QString bindir = this->CMakeThread->cmakeInstance()->binaryDirectory();
  317. QDir dir(bindir);
  318. if (!dir.exists()) {
  319. QString msg = tr("Build directory does not exist, "
  320. "should I create it?\n\n"
  321. "Directory: ");
  322. msg += bindir;
  323. QString title = tr("Create Directory");
  324. QMessageBox::StandardButton btn;
  325. btn = QMessageBox::information(this, title, msg,
  326. QMessageBox::Yes | QMessageBox::No);
  327. if (btn == QMessageBox::No) {
  328. return false;
  329. }
  330. if (!dir.mkpath(".")) {
  331. QMessageBox::information(
  332. this, tr("Create Directory Failed"),
  333. QString(tr("Failed to create directory %1")).arg(dir.path()),
  334. QMessageBox::Ok);
  335. return false;
  336. }
  337. }
  338. // if no generator, prompt for it and other setup stuff
  339. if (this->CMakeThread->cmakeInstance()->generator().isEmpty()) {
  340. if (!this->setupFirstConfigure()) {
  341. return false;
  342. }
  343. }
  344. // remember path
  345. this->addBinaryPath(dir.absolutePath());
  346. return true;
  347. }
  348. void CMakeSetupDialog::exitLoop(int err)
  349. {
  350. this->LocalLoop.exit(err);
  351. }
  352. void CMakeSetupDialog::doConfigure()
  353. {
  354. if (this->CurrentState == Configuring) {
  355. // stop configure
  356. doInterrupt();
  357. return;
  358. }
  359. if (!prepareConfigure()) {
  360. return;
  361. }
  362. this->enterState(Configuring);
  363. bool ret = doConfigureInternal();
  364. if (ret) {
  365. this->ConfigureNeeded = false;
  366. }
  367. if (ret && !this->CacheValues->cacheModel()->newPropertyCount()) {
  368. this->enterState(ReadyGenerate);
  369. } else {
  370. this->enterState(ReadyConfigure);
  371. this->CacheValues->scrollToTop();
  372. }
  373. this->ProgressBar->reset();
  374. #ifdef QT_WINEXTRAS
  375. this->TaskbarButton->progress()->reset();
  376. #endif
  377. }
  378. bool CMakeSetupDialog::doConfigureInternal()
  379. {
  380. this->Output->clear();
  381. this->CacheValues->selectionModel()->clear();
  382. QMetaObject::invokeMethod(
  383. this->CMakeThread->cmakeInstance(), "setProperties", Qt::QueuedConnection,
  384. Q_ARG(QCMakePropertyList, this->CacheValues->cacheModel()->properties()));
  385. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(), "configure",
  386. Qt::QueuedConnection);
  387. int err = this->LocalLoop.exec();
  388. if (err != 0) {
  389. QMessageBox::critical(
  390. this, tr("Error"),
  391. tr("Error in configuration process, project files may be invalid"),
  392. QMessageBox::Ok);
  393. }
  394. return 0 == err;
  395. }
  396. void CMakeSetupDialog::doInstallForCommandLine()
  397. {
  398. QString title = tr("How to Install For Command Line Use");
  399. QString msg = tr("One may add CMake to the PATH:\n"
  400. "\n"
  401. " PATH=\"%1\":\"$PATH\"\n"
  402. "\n"
  403. "Or, to install symlinks to '/usr/local/bin', run:\n"
  404. "\n"
  405. " sudo \"%2\" --install\n"
  406. "\n"
  407. "Or, to install symlinks to another directory, run:\n"
  408. "\n"
  409. " sudo \"%3\" --install=/path/to/bin\n");
  410. msg = msg.arg(
  411. cmSystemTools::GetFilenamePath(cmSystemTools::GetCMakeCommand()).c_str());
  412. msg = msg.arg(cmSystemTools::GetCMakeGUICommand().c_str());
  413. msg = msg.arg(cmSystemTools::GetCMakeGUICommand().c_str());
  414. QDialog dialog;
  415. dialog.setWindowTitle(title);
  416. QVBoxLayout* l = new QVBoxLayout(&dialog);
  417. QLabel* lab = new QLabel(&dialog);
  418. l->addWidget(lab);
  419. lab->setText(msg);
  420. lab->setWordWrap(false);
  421. lab->setTextInteractionFlags(Qt::TextSelectableByMouse);
  422. QDialogButtonBox* btns =
  423. new QDialogButtonBox(QDialogButtonBox::Ok, Qt::Horizontal, &dialog);
  424. QObject::connect(btns, &QDialogButtonBox::accepted, &dialog,
  425. &QDialog::accept);
  426. l->addWidget(btns);
  427. dialog.exec();
  428. }
  429. bool CMakeSetupDialog::doGenerateInternal()
  430. {
  431. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(), "generate",
  432. Qt::QueuedConnection);
  433. int err = this->LocalLoop.exec();
  434. if (err != 0) {
  435. QMessageBox::critical(
  436. this, tr("Error"),
  437. tr("Error in generation process, project files may be invalid"),
  438. QMessageBox::Ok);
  439. }
  440. return 0 == err;
  441. }
  442. void CMakeSetupDialog::doGenerate()
  443. {
  444. if (this->CurrentState == Generating) {
  445. // stop generate
  446. doInterrupt();
  447. return;
  448. }
  449. // see if we need to configure
  450. // we'll need to configure if:
  451. // the configure step hasn't been done yet
  452. // generate was the last step done
  453. if (this->ConfigureNeeded) {
  454. if (!prepareConfigure()) {
  455. return;
  456. }
  457. }
  458. this->enterState(Generating);
  459. bool config_passed = true;
  460. if (this->ConfigureNeeded) {
  461. this->CacheValues->cacheModel()->setShowNewProperties(false);
  462. this->ProgressFactor = 0.5;
  463. config_passed = doConfigureInternal();
  464. this->ProgressOffset = 0.5;
  465. }
  466. if (config_passed) {
  467. doGenerateInternal();
  468. }
  469. this->ProgressOffset = 0.0;
  470. this->ProgressFactor = 1.0;
  471. this->CacheValues->cacheModel()->setShowNewProperties(true);
  472. this->enterState(ReadyConfigure);
  473. this->ProgressBar->reset();
  474. #ifdef QT_WINEXTRAS
  475. this->TaskbarButton->progress()->reset();
  476. #endif
  477. this->ConfigureNeeded = true;
  478. }
  479. void CMakeSetupDialog::doOpenProject()
  480. {
  481. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(), "open",
  482. Qt::QueuedConnection);
  483. }
  484. void CMakeSetupDialog::closeEvent(QCloseEvent* e)
  485. {
  486. // prompt for close if there are unsaved changes, and we're not busy
  487. if (this->CacheModified) {
  488. QString msg = tr("You have changed options but not rebuilt, "
  489. "are you sure you want to exit?");
  490. QString title = tr("Confirm Exit");
  491. QMessageBox::StandardButton btn;
  492. btn = QMessageBox::critical(this, title, msg,
  493. QMessageBox::Yes | QMessageBox::No);
  494. if (btn == QMessageBox::No) {
  495. e->ignore();
  496. }
  497. }
  498. // don't close if we're busy, unless the user really wants to
  499. if (this->CurrentState == Configuring) {
  500. QString msg =
  501. tr("You are in the middle of a Configure.\n"
  502. "If you Exit now the configure information will be lost.\n"
  503. "Are you sure you want to Exit?");
  504. QString title = tr("Confirm Exit");
  505. QMessageBox::StandardButton btn;
  506. btn = QMessageBox::critical(this, title, msg,
  507. QMessageBox::Yes | QMessageBox::No);
  508. if (btn == QMessageBox::No) {
  509. e->ignore();
  510. } else {
  511. this->doInterrupt();
  512. }
  513. }
  514. // let the generate finish
  515. if (this->CurrentState == Generating) {
  516. e->ignore();
  517. }
  518. }
  519. void CMakeSetupDialog::doHelp()
  520. {
  521. QString msg = tr(
  522. "CMake is used to configure and generate build files for "
  523. "software projects. The basic steps for configuring a project are as "
  524. "follows:\r\n\r\n1. Select the source directory for the project. This "
  525. "should "
  526. "contain the CMakeLists.txt files for the project.\r\n\r\n2. Select the "
  527. "build "
  528. "directory for the project. This is the directory where the project "
  529. "will be "
  530. "built. It can be the same or a different directory than the source "
  531. "directory. For easy clean up, a separate build directory is "
  532. "recommended. "
  533. "CMake will create the directory if it does not exist.\r\n\r\n3. Once the "
  534. "source and binary directories are selected, it is time to press the "
  535. "Configure button. This will cause CMake to read all of the input files "
  536. "and "
  537. "discover all the variables used by the project. The first time a "
  538. "variable "
  539. "is displayed it will be in Red. Users should inspect red variables "
  540. "making "
  541. "sure the values are correct. For some projects the Configure process "
  542. "can "
  543. "be iterative, so continue to press the Configure button until there are "
  544. "no "
  545. "longer red entries.\r\n\r\n4. Once there are no longer red entries, you "
  546. "should click the Generate button. This will write the build files to "
  547. "the build "
  548. "directory.");
  549. QDialog dialog;
  550. QFontMetrics met(this->font());
  551. #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
  552. int msgWidth = met.horizontalAdvance(msg);
  553. #else
  554. int msgWidth = met.width(msg);
  555. #endif
  556. dialog.setMinimumSize(msgWidth / 15, 20);
  557. dialog.setWindowTitle(tr("Help"));
  558. QVBoxLayout* l = new QVBoxLayout(&dialog);
  559. QLabel* lab = new QLabel(&dialog);
  560. lab->setText(msg);
  561. lab->setWordWrap(true);
  562. QDialogButtonBox* btns =
  563. new QDialogButtonBox(QDialogButtonBox::Ok, Qt::Horizontal, &dialog);
  564. QObject::connect(btns, &QDialogButtonBox::accepted, &dialog,
  565. &QDialog::accept);
  566. l->addWidget(lab);
  567. l->addWidget(btns);
  568. dialog.exec();
  569. }
  570. void CMakeSetupDialog::doInterrupt()
  571. {
  572. this->enterState(Interrupting);
  573. this->CMakeThread->cmakeInstance()->interrupt();
  574. }
  575. void CMakeSetupDialog::doSourceBrowse()
  576. {
  577. QString dir = QFileDialog::getExistingDirectory(
  578. this, tr("Enter Path to Source"), this->SourceDirectory->text(),
  579. QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
  580. if (!dir.isEmpty()) {
  581. this->setSourceDirectory(dir);
  582. }
  583. }
  584. void CMakeSetupDialog::updateSourceDirectory(const QString& dir)
  585. {
  586. if (this->SourceDirectory->text() != dir) {
  587. this->SourceDirectory->blockSignals(true);
  588. this->SourceDirectory->setText(dir);
  589. this->SourceDirectory->blockSignals(false);
  590. }
  591. }
  592. void CMakeSetupDialog::updateBinaryDirectory(const QString& dir)
  593. {
  594. if (this->BinaryDirectory->currentText() != dir) {
  595. this->BinaryDirectory->blockSignals(true);
  596. this->BinaryDirectory->setEditText(dir);
  597. this->BinaryDirectory->blockSignals(false);
  598. }
  599. }
  600. void CMakeSetupDialog::updatePresets(const QVector<QCMakePreset>& presets)
  601. {
  602. if (this->Preset->presets() != presets) {
  603. this->Preset->blockSignals(true);
  604. this->Preset->setPresets(presets);
  605. this->Preset->blockSignals(false);
  606. }
  607. this->Preset->setDisabled(presets.isEmpty());
  608. this->Preset->setToolTip(presets.isEmpty() ? PRESETS_DISABLED_TOOLTIP : "");
  609. if (!this->DeferredPreset.isNull()) {
  610. this->Preset->setPresetName(this->DeferredPreset);
  611. this->DeferredPreset = QString{};
  612. }
  613. }
  614. void CMakeSetupDialog::updatePreset(const QString& name)
  615. {
  616. if (this->Preset->presetName() != name) {
  617. this->Preset->blockSignals(true);
  618. this->Preset->setPresetName(name);
  619. this->Preset->blockSignals(false);
  620. }
  621. }
  622. void CMakeSetupDialog::showPresetLoadError(
  623. const QString& dir, cmCMakePresetsFile::ReadFileResult result)
  624. {
  625. QMessageBox::warning(
  626. this, "Error Reading CMake Presets",
  627. QString::fromLocal8Bit("Could not read presets from %1: %2")
  628. .arg(dir, cmCMakePresetsFile::ResultToString(result)));
  629. }
  630. void CMakeSetupDialog::doBinaryBrowse()
  631. {
  632. QString dir = QFileDialog::getExistingDirectory(
  633. this, tr("Enter Path to Build"), this->BinaryDirectory->currentText(),
  634. QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
  635. if (!dir.isEmpty() && dir != this->BinaryDirectory->currentText()) {
  636. this->setBinaryDirectory(dir);
  637. }
  638. }
  639. void CMakeSetupDialog::setBinaryDirectory(const QString& dir)
  640. {
  641. this->BinaryDirectory->setEditText(dir);
  642. }
  643. void CMakeSetupDialog::setStartupBinaryDirectory(bool startup)
  644. {
  645. this->StartupBinaryDirectory = startup;
  646. }
  647. void CMakeSetupDialog::onSourceDirectoryChanged(const QString& dir)
  648. {
  649. this->Output->clear();
  650. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  651. "setSourceDirectory", Qt::QueuedConnection,
  652. Q_ARG(QString, dir));
  653. }
  654. void CMakeSetupDialog::onBinaryDirectoryChanged(const QString& dir)
  655. {
  656. QString title = QString(tr("CMake %1 - %2"));
  657. title = title.arg(cmVersion::GetCMakeVersion());
  658. title = title.arg(dir);
  659. this->setWindowTitle(title);
  660. this->CacheModified = false;
  661. this->CacheValues->cacheModel()->clear();
  662. qobject_cast<QCMakeCacheModelDelegate*>(this->CacheValues->itemDelegate())
  663. ->clearChanges();
  664. this->Output->clear();
  665. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  666. "setBinaryDirectory", Qt::QueuedConnection,
  667. Q_ARG(QString, dir));
  668. }
  669. void CMakeSetupDialog::onBuildPresetChanged(const QString& name)
  670. {
  671. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(), "setPreset",
  672. Qt::QueuedConnection, Q_ARG(QString, name),
  673. Q_ARG(bool, !this->StartupBinaryDirectory));
  674. this->StartupBinaryDirectory = false;
  675. }
  676. void CMakeSetupDialog::setSourceDirectory(const QString& dir)
  677. {
  678. this->SourceDirectory->setText(dir);
  679. }
  680. void CMakeSetupDialog::setDeferredPreset(const QString& preset)
  681. {
  682. this->DeferredPreset = preset;
  683. }
  684. void CMakeSetupDialog::showProgress(const QString& /*msg*/, float percent)
  685. {
  686. percent = (percent * ProgressFactor) + ProgressOffset;
  687. this->ProgressBar->setValue(qRound(percent * 100));
  688. #ifdef QT_WINEXTRAS
  689. QWinTaskbarProgress* progress = this->TaskbarButton->progress();
  690. progress->setVisible(true);
  691. progress->setValue(qRound(percent * 100));
  692. #endif
  693. }
  694. void CMakeSetupDialog::error(const QString& msg)
  695. {
  696. this->Output->setCurrentCharFormat(this->ErrorFormat);
  697. // QTextEdit will terminate the msg with a ParagraphSeparator, but it also
  698. // replaces
  699. // all newlines with ParagraphSeparators. By replacing the newlines by
  700. // ourself, one
  701. // error msg will be one paragraph.
  702. QString paragraph(msg);
  703. paragraph.replace(QLatin1Char('\n'), QChar::LineSeparator);
  704. this->Output->append(paragraph);
  705. }
  706. void CMakeSetupDialog::message(const QString& msg)
  707. {
  708. this->Output->setCurrentCharFormat(this->MessageFormat);
  709. this->Output->append(msg);
  710. }
  711. void CMakeSetupDialog::setEnabledState(bool enabled)
  712. {
  713. // disable parts of the GUI during configure/generate
  714. this->CacheValues->cacheModel()->setEditEnabled(enabled);
  715. this->SourceDirectory->setEnabled(enabled);
  716. this->BrowseSourceDirectoryButton->setEnabled(enabled);
  717. this->Preset->setEnabled(enabled && !this->Preset->presets().isEmpty());
  718. this->BinaryDirectory->setEnabled(enabled);
  719. this->BrowseBinaryDirectoryButton->setEnabled(enabled);
  720. this->ReloadCacheAction->setEnabled(enabled);
  721. this->DeleteCacheAction->setEnabled(enabled);
  722. this->ExitAction->setEnabled(enabled);
  723. this->ConfigureAction->setEnabled(enabled);
  724. this->AddEntry->setEnabled(enabled);
  725. this->RemoveEntry->setEnabled(false); // let selection re-enable it
  726. this->Environment->setEnabled(enabled);
  727. }
  728. bool CMakeSetupDialog::setupFirstConfigure()
  729. {
  730. FirstConfigure dialog;
  731. // initialize dialog and restore saved settings
  732. // add generators
  733. dialog.setGenerators(
  734. this->CMakeThread->cmakeInstance()->availableGenerators());
  735. // restore from settings
  736. dialog.loadFromSettings();
  737. auto presetData = this->Preset->currentData();
  738. if (presetData.isValid()) {
  739. auto preset = presetData.value<QCMakePreset>();
  740. dialog.setCurrentGenerator(preset.generator);
  741. if (preset.setGenConfig) {
  742. dialog.setPlatform(preset.architecture);
  743. dialog.setToolset(preset.toolset);
  744. }
  745. dialog.setCompilerOption(CompilerOption::DefaultNative);
  746. }
  747. if (dialog.exec() == QDialog::Accepted) {
  748. dialog.saveToSettings();
  749. this->CMakeThread->cmakeInstance()->setGenerator(dialog.getGenerator());
  750. this->CMakeThread->cmakeInstance()->setPlatform(dialog.getPlatform());
  751. this->CMakeThread->cmakeInstance()->setToolset(dialog.getToolset());
  752. QCMakeCacheModel* m = this->CacheValues->cacheModel();
  753. if (dialog.compilerSetup()) {
  754. QString fortranCompiler = dialog.getFortranCompiler();
  755. if (!fortranCompiler.isEmpty()) {
  756. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_Fortran_COMPILER",
  757. "Fortran compiler.", fortranCompiler, false);
  758. }
  759. QString cxxCompiler = dialog.getCXXCompiler();
  760. if (!cxxCompiler.isEmpty()) {
  761. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_CXX_COMPILER",
  762. "CXX compiler.", cxxCompiler, false);
  763. }
  764. QString cCompiler = dialog.getCCompiler();
  765. if (!cCompiler.isEmpty()) {
  766. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_C_COMPILER",
  767. "C compiler.", cCompiler, false);
  768. }
  769. } else if (dialog.crossCompilerSetup()) {
  770. QString fortranCompiler = dialog.getFortranCompiler();
  771. if (!fortranCompiler.isEmpty()) {
  772. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_Fortran_COMPILER",
  773. "Fortran compiler.", fortranCompiler, false);
  774. }
  775. QString mode = dialog.getCrossIncludeMode();
  776. m->insertProperty(QCMakeProperty::STRING,
  777. "CMAKE_FIND_ROOT_PATH_MODE_INCLUDE",
  778. tr("CMake Find Include Mode"), mode, false);
  779. mode = dialog.getCrossLibraryMode();
  780. m->insertProperty(QCMakeProperty::STRING,
  781. "CMAKE_FIND_ROOT_PATH_MODE_LIBRARY",
  782. tr("CMake Find Library Mode"), mode, false);
  783. mode = dialog.getCrossProgramMode();
  784. m->insertProperty(QCMakeProperty::STRING,
  785. "CMAKE_FIND_ROOT_PATH_MODE_PROGRAM",
  786. tr("CMake Find Program Mode"), mode, false);
  787. QString rootPath = dialog.getCrossRoot();
  788. m->insertProperty(QCMakeProperty::PATH, "CMAKE_FIND_ROOT_PATH",
  789. tr("CMake Find Root Path"), rootPath, false);
  790. QString systemName = dialog.getSystemName();
  791. m->insertProperty(QCMakeProperty::STRING, "CMAKE_SYSTEM_NAME",
  792. tr("CMake System Name"), systemName, false);
  793. QString systemVersion = dialog.getSystemVersion();
  794. m->insertProperty(QCMakeProperty::STRING, "CMAKE_SYSTEM_VERSION",
  795. tr("CMake System Version"), systemVersion, false);
  796. QString systemProcessor = dialog.getSystemProcessor();
  797. m->insertProperty(QCMakeProperty::STRING, "CMAKE_SYSTEM_PROCESSOR",
  798. tr("CMake System Processor"), systemProcessor, false);
  799. QString cxxCompiler = dialog.getCXXCompiler();
  800. if (!cxxCompiler.isEmpty()) {
  801. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_CXX_COMPILER",
  802. tr("CXX compiler."), cxxCompiler, false);
  803. }
  804. QString cCompiler = dialog.getCCompiler();
  805. if (!cCompiler.isEmpty()) {
  806. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_C_COMPILER",
  807. tr("C compiler."), cCompiler, false);
  808. }
  809. } else if (dialog.crossCompilerToolChainFile()) {
  810. QString toolchainFile = dialog.getCrossCompilerToolChainFile();
  811. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_TOOLCHAIN_FILE",
  812. tr("Cross Compile ToolChain File"), toolchainFile,
  813. false);
  814. }
  815. return true;
  816. }
  817. return false;
  818. }
  819. void CMakeSetupDialog::updateGeneratorLabel(const QString& gen)
  820. {
  821. QString str = tr("Current Generator: ");
  822. if (gen.isEmpty()) {
  823. str += tr("None");
  824. } else {
  825. str += gen;
  826. }
  827. this->Generator->setText(str);
  828. }
  829. void CMakeSetupDialog::doReloadCache()
  830. {
  831. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(), "reloadCache",
  832. Qt::QueuedConnection);
  833. }
  834. void CMakeSetupDialog::doDeleteCache()
  835. {
  836. QString title = tr("Delete Cache");
  837. QString msg = tr("Are you sure you want to delete the cache?");
  838. QMessageBox::StandardButton btn;
  839. btn = QMessageBox::information(this, title, msg,
  840. QMessageBox::Yes | QMessageBox::No);
  841. if (btn == QMessageBox::No) {
  842. return;
  843. }
  844. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(), "deleteCache",
  845. Qt::QueuedConnection);
  846. }
  847. void CMakeSetupDialog::doAbout()
  848. {
  849. QString msg = tr(
  850. "CMake %1 (cmake.org).\n"
  851. "CMake suite maintained and supported by Kitware (kitware.com/cmake).\n"
  852. "Distributed under terms of the BSD 3-Clause License.\n"
  853. "\n"
  854. "CMake GUI maintained by csimsoft,\n"
  855. "built using Qt %2 (qt-project.org).\n"
  856. #ifdef USE_LGPL
  857. "\n"
  858. "The Qt Toolkit is Copyright (C) The Qt Company Ltd.\n"
  859. "Qt is licensed under terms of the GNU LGPLv" USE_LGPL ", available at:\n"
  860. " \"%3\""
  861. #endif
  862. );
  863. msg = msg.arg(cmVersion::GetCMakeVersion());
  864. msg = msg.arg(qVersion());
  865. #ifdef USE_LGPL
  866. std::string lgpl =
  867. cmSystemTools::GetCMakeRoot() + "/Licenses/LGPLv" USE_LGPL ".txt";
  868. msg = msg.arg(lgpl.c_str());
  869. #endif
  870. QDialog dialog;
  871. dialog.setWindowTitle(tr("About"));
  872. QVBoxLayout* l = new QVBoxLayout(&dialog);
  873. QLabel* lab = new QLabel(&dialog);
  874. l->addWidget(lab);
  875. lab->setText(msg);
  876. lab->setWordWrap(true);
  877. QDialogButtonBox* btns =
  878. new QDialogButtonBox(QDialogButtonBox::Ok, Qt::Horizontal, &dialog);
  879. QObject::connect(btns, &QDialogButtonBox::accepted, &dialog,
  880. &QDialog::accept);
  881. l->addWidget(btns);
  882. dialog.exec();
  883. }
  884. void CMakeSetupDialog::setExitAfterGenerate(bool b)
  885. {
  886. this->ExitAfterGenerate = b;
  887. }
  888. void CMakeSetupDialog::addBinaryPath(const QString& path)
  889. {
  890. QString cleanpath = QDir::cleanPath(path);
  891. // update UI
  892. this->BinaryDirectory->blockSignals(true);
  893. int idx = this->BinaryDirectory->findText(cleanpath);
  894. if (idx != -1) {
  895. this->BinaryDirectory->removeItem(idx);
  896. }
  897. this->BinaryDirectory->insertItem(0, cleanpath);
  898. this->BinaryDirectory->setCurrentIndex(0);
  899. this->BinaryDirectory->blockSignals(false);
  900. // save to registry
  901. QStringList buildPaths = this->loadBuildPaths();
  902. buildPaths.removeAll(cleanpath);
  903. buildPaths.prepend(cleanpath);
  904. this->saveBuildPaths(buildPaths);
  905. }
  906. void CMakeSetupDialog::dragEnterEvent(QDragEnterEvent* e)
  907. {
  908. if (!(this->CurrentState == ReadyConfigure ||
  909. this->CurrentState == ReadyGenerate)) {
  910. e->ignore();
  911. return;
  912. }
  913. const QMimeData* dat = e->mimeData();
  914. QList<QUrl> urls = dat->urls();
  915. QString file = urls.count() ? urls[0].toLocalFile() : QString();
  916. if (!file.isEmpty() &&
  917. (file.endsWith("CMakeCache.txt", Qt::CaseInsensitive) ||
  918. file.endsWith("CMakeLists.txt", Qt::CaseInsensitive))) {
  919. e->accept();
  920. } else {
  921. e->ignore();
  922. }
  923. }
  924. void CMakeSetupDialog::dropEvent(QDropEvent* e)
  925. {
  926. if (!(this->CurrentState == ReadyConfigure ||
  927. this->CurrentState == ReadyGenerate)) {
  928. return;
  929. }
  930. const QMimeData* dat = e->mimeData();
  931. QList<QUrl> urls = dat->urls();
  932. QString file = urls.count() ? urls[0].toLocalFile() : QString();
  933. if (file.endsWith("CMakeCache.txt", Qt::CaseInsensitive)) {
  934. QFileInfo info(file);
  935. if (this->CMakeThread->cmakeInstance()->binaryDirectory() !=
  936. info.absolutePath()) {
  937. this->setBinaryDirectory(info.absolutePath());
  938. }
  939. } else if (file.endsWith("CMakeLists.txt", Qt::CaseInsensitive)) {
  940. QFileInfo info(file);
  941. if (this->CMakeThread->cmakeInstance()->binaryDirectory() !=
  942. info.absolutePath()) {
  943. this->setSourceDirectory(info.absolutePath());
  944. this->setBinaryDirectory(info.absolutePath());
  945. }
  946. }
  947. }
  948. QStringList CMakeSetupDialog::loadBuildPaths()
  949. {
  950. QSettings settings;
  951. settings.beginGroup("Settings/StartPath");
  952. QStringList buildPaths;
  953. for (int i = 0; i < 10; i++) {
  954. QString p = settings.value(QString("WhereBuild%1").arg(i)).toString();
  955. if (!p.isEmpty()) {
  956. buildPaths.append(p);
  957. }
  958. }
  959. return buildPaths;
  960. }
  961. void CMakeSetupDialog::saveBuildPaths(const QStringList& paths)
  962. {
  963. QSettings settings;
  964. settings.beginGroup("Settings/StartPath");
  965. int num = paths.count();
  966. if (num > 10) {
  967. num = 10;
  968. }
  969. for (int i = 0; i < num; i++) {
  970. settings.setValue(QString("WhereBuild%1").arg(i), paths[i]);
  971. }
  972. }
  973. void CMakeSetupDialog::setCacheModified()
  974. {
  975. this->CacheModified = true;
  976. this->ConfigureNeeded = true;
  977. this->enterState(ReadyConfigure);
  978. }
  979. void CMakeSetupDialog::removeSelectedCacheEntries()
  980. {
  981. QModelIndexList idxs = this->CacheValues->selectionModel()->selectedRows();
  982. QList<QPersistentModelIndex> pidxs;
  983. foreach (QModelIndex const& i, idxs) {
  984. pidxs.append(i);
  985. }
  986. foreach (QPersistentModelIndex const& pi, pidxs) {
  987. this->CacheValues->model()->removeRow(pi.row(), pi.parent());
  988. }
  989. }
  990. void CMakeSetupDialog::selectionChanged()
  991. {
  992. QModelIndexList idxs = this->CacheValues->selectionModel()->selectedRows();
  993. if (idxs.count() &&
  994. (this->CurrentState == ReadyConfigure ||
  995. this->CurrentState == ReadyGenerate)) {
  996. this->RemoveEntry->setEnabled(true);
  997. } else {
  998. this->RemoveEntry->setEnabled(false);
  999. }
  1000. }
  1001. void CMakeSetupDialog::enterState(CMakeSetupDialog::State s)
  1002. {
  1003. if (s == this->CurrentState) {
  1004. return;
  1005. }
  1006. this->CurrentState = s;
  1007. if (s == Interrupting) {
  1008. this->ConfigureButton->setEnabled(false);
  1009. this->GenerateButton->setEnabled(false);
  1010. this->OpenProjectButton->setEnabled(false);
  1011. } else if (s == Configuring) {
  1012. this->setEnabledState(false);
  1013. this->GenerateButton->setEnabled(false);
  1014. this->GenerateAction->setEnabled(false);
  1015. this->OpenProjectButton->setEnabled(false);
  1016. this->ConfigureButton->setText(tr("&Stop"));
  1017. } else if (s == Generating) {
  1018. this->CacheModified = false;
  1019. this->setEnabledState(false);
  1020. this->ConfigureButton->setEnabled(false);
  1021. this->GenerateAction->setEnabled(false);
  1022. this->OpenProjectButton->setEnabled(false);
  1023. this->GenerateButton->setText(tr("&Stop"));
  1024. } else if (s == ReadyConfigure || s == ReadyGenerate) {
  1025. this->setEnabledState(true);
  1026. this->GenerateButton->setEnabled(true);
  1027. this->GenerateAction->setEnabled(true);
  1028. this->ConfigureButton->setEnabled(true);
  1029. this->ConfigureButton->setText(tr("&Configure"));
  1030. this->GenerateButton->setText(tr("&Generate"));
  1031. }
  1032. }
  1033. void CMakeSetupDialog::editEnvironment()
  1034. {
  1035. EnvironmentDialog dialog(this->CMakeThread->cmakeInstance()->environment(),
  1036. this);
  1037. if (dialog.exec() == QDialog::Accepted) {
  1038. QMetaObject::invokeMethod(
  1039. this->CMakeThread->cmakeInstance(), "setEnvironment",
  1040. Q_ARG(QProcessEnvironment, dialog.environment()));
  1041. }
  1042. }
  1043. void CMakeSetupDialog::addCacheEntry()
  1044. {
  1045. QDialog dialog(this);
  1046. dialog.resize(400, 200);
  1047. dialog.setWindowTitle(tr("Add Cache Entry"));
  1048. QVBoxLayout* l = new QVBoxLayout(&dialog);
  1049. AddCacheEntry* w =
  1050. new AddCacheEntry(&dialog, this->AddVariableNames, this->AddVariableTypes);
  1051. QDialogButtonBox* btns = new QDialogButtonBox(
  1052. QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &dialog);
  1053. QObject::connect(btns, &QDialogButtonBox::accepted, &dialog,
  1054. &QDialog::accept);
  1055. QObject::connect(btns, &QDialogButtonBox::rejected, &dialog,
  1056. &QDialog::reject);
  1057. l->addWidget(w);
  1058. l->addStretch();
  1059. l->addWidget(btns);
  1060. if (QDialog::Accepted == dialog.exec()) {
  1061. QCMakeCacheModel* m = this->CacheValues->cacheModel();
  1062. m->insertProperty(w->type(), w->name(), w->description(), w->value(),
  1063. false);
  1064. // only add variable names to the completion which are new
  1065. if (!this->AddVariableNames.contains(w->name())) {
  1066. this->AddVariableNames << w->name();
  1067. this->AddVariableTypes << w->typeString();
  1068. // limit to at most 100 completion items
  1069. if (this->AddVariableNames.size() > 100) {
  1070. this->AddVariableNames.removeFirst();
  1071. this->AddVariableTypes.removeFirst();
  1072. }
  1073. // make sure CMAKE_INSTALL_PREFIX is always there
  1074. if (!this->AddVariableNames.contains("CMAKE_INSTALL_PREFIX")) {
  1075. this->AddVariableNames << "CMAKE_INSTALL_PREFIX";
  1076. this->AddVariableTypes << "PATH";
  1077. }
  1078. QSettings settings;
  1079. settings.beginGroup("Settings/StartPath");
  1080. settings.setValue("AddVariableNames", this->AddVariableNames);
  1081. settings.setValue("AddVariableTypes", this->AddVariableTypes);
  1082. }
  1083. }
  1084. }
  1085. void CMakeSetupDialog::startSearch()
  1086. {
  1087. this->Search->setFocus(Qt::OtherFocusReason);
  1088. this->Search->selectAll();
  1089. }
  1090. void CMakeSetupDialog::setDebugOutput(bool flag)
  1091. {
  1092. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  1093. "setDebugOutput", Qt::QueuedConnection,
  1094. Q_ARG(bool, flag));
  1095. }
  1096. void CMakeSetupDialog::setGroupedView(bool v)
  1097. {
  1098. this->CacheValues->cacheModel()->setViewType(v ? QCMakeCacheModel::GroupView
  1099. : QCMakeCacheModel::FlatView);
  1100. this->CacheValues->setRootIsDecorated(v);
  1101. QSettings settings;
  1102. settings.beginGroup("Settings/StartPath");
  1103. settings.setValue("GroupView", v);
  1104. }
  1105. void CMakeSetupDialog::setAdvancedView(bool v)
  1106. {
  1107. this->CacheValues->setShowAdvanced(v);
  1108. QSettings settings;
  1109. settings.beginGroup("Settings/StartPath");
  1110. settings.setValue("AdvancedView", v);
  1111. }
  1112. void CMakeSetupDialog::showUserChanges()
  1113. {
  1114. QSet<QCMakeProperty> changes =
  1115. qobject_cast<QCMakeCacheModelDelegate*>(this->CacheValues->itemDelegate())
  1116. ->changes();
  1117. QDialog dialog(this);
  1118. dialog.setWindowTitle(tr("My Changes"));
  1119. dialog.resize(600, 400);
  1120. QVBoxLayout* l = new QVBoxLayout(&dialog);
  1121. QTextEdit* textedit = new QTextEdit(&dialog);
  1122. textedit->setReadOnly(true);
  1123. l->addWidget(textedit);
  1124. QDialogButtonBox* btns =
  1125. new QDialogButtonBox(QDialogButtonBox::Close, Qt::Horizontal, &dialog);
  1126. QObject::connect(btns, &QDialogButtonBox::rejected, &dialog,
  1127. &QDialog::accept);
  1128. l->addWidget(btns);
  1129. QString command;
  1130. QString cache;
  1131. foreach (QCMakeProperty const& prop, changes) {
  1132. QString type;
  1133. switch (prop.Type) {
  1134. case QCMakeProperty::BOOL:
  1135. type = "BOOL";
  1136. break;
  1137. case QCMakeProperty::PATH:
  1138. type = "PATH";
  1139. break;
  1140. case QCMakeProperty::FILEPATH:
  1141. type = "FILEPATH";
  1142. break;
  1143. case QCMakeProperty::STRING:
  1144. type = "STRING";
  1145. break;
  1146. }
  1147. QString value;
  1148. if (prop.Type == QCMakeProperty::BOOL) {
  1149. value = prop.Value.toBool() ? "1" : "0";
  1150. } else {
  1151. value = prop.Value.toString();
  1152. }
  1153. QString const line = QString("%1:%2=").arg(prop.Key, type);
  1154. command += QString("-D%1\"%2\" ").arg(line, value);
  1155. cache += QString("%1%2\n").arg(line, value);
  1156. }
  1157. textedit->append(tr("Commandline options:"));
  1158. textedit->append(command);
  1159. textedit->append("\n");
  1160. textedit->append(tr("Cache file:"));
  1161. textedit->append(cache);
  1162. dialog.exec();
  1163. }
  1164. void CMakeSetupDialog::setSearchFilter(const QString& str)
  1165. {
  1166. this->CacheValues->selectionModel()->clear();
  1167. this->CacheValues->setSearchFilter(str);
  1168. }
  1169. void CMakeSetupDialog::doOutputContextMenu(QPoint pt)
  1170. {
  1171. std::unique_ptr<QMenu> menu(this->Output->createStandardContextMenu());
  1172. menu->addSeparator();
  1173. auto* a = menu->addAction(tr("Find..."));
  1174. QObject::connect(a, &QAction::triggered, this,
  1175. &CMakeSetupDialog::doOutputFindDialog);
  1176. a->setShortcut(QKeySequence::Find);
  1177. a = menu->addAction(tr("Find Next"));
  1178. QObject::connect(a, &QAction::triggered, this,
  1179. &CMakeSetupDialog::doOutputFindNext);
  1180. a->setShortcut(QKeySequence::FindNext);
  1181. a = menu->addAction(tr("Find Previous"));
  1182. QObject::connect(a, &QAction::triggered, this,
  1183. &CMakeSetupDialog::doOutputFindPrev);
  1184. a->setShortcut(QKeySequence::FindPrevious);
  1185. menu->addSeparator();
  1186. a = menu->addAction(tr("Goto Next Error"));
  1187. QObject::connect(a, &QAction::triggered, this,
  1188. &CMakeSetupDialog::doOutputErrorNext);
  1189. a->setShortcut(QKeySequence(Qt::Key_F8));
  1190. menu->exec(this->Output->mapToGlobal(pt));
  1191. }
  1192. void CMakeSetupDialog::doOutputFindDialog()
  1193. {
  1194. QStringList strings(this->FindHistory);
  1195. QString selection = this->Output->textCursor().selectedText();
  1196. if (!selection.isEmpty() && !selection.contains(QChar::ParagraphSeparator) &&
  1197. !selection.contains(QChar::LineSeparator)) {
  1198. strings.push_front(selection);
  1199. }
  1200. bool ok;
  1201. QString search = QInputDialog::getItem(this, tr("Find in Output"),
  1202. tr("Find:"), strings, 0, true, &ok);
  1203. if (ok && !search.isEmpty()) {
  1204. if (!this->FindHistory.contains(search)) {
  1205. this->FindHistory.push_front(search);
  1206. }
  1207. doOutputFindNext();
  1208. }
  1209. }
  1210. void CMakeSetupDialog::doRegexExplorerDialog()
  1211. {
  1212. RegexExplorer dialog(this);
  1213. dialog.exec();
  1214. }
  1215. void CMakeSetupDialog::doOutputFindPrev()
  1216. {
  1217. doOutputFindNext(false);
  1218. }
  1219. void CMakeSetupDialog::doOutputFindNext(bool directionForward)
  1220. {
  1221. if (this->FindHistory.isEmpty()) {
  1222. doOutputFindDialog(); // will re-call this function again
  1223. return;
  1224. }
  1225. QString search = this->FindHistory.front();
  1226. QTextCursor textCursor = this->Output->textCursor();
  1227. QTextDocument* document = this->Output->document();
  1228. QTextDocument::FindFlags flags;
  1229. if (!directionForward) {
  1230. flags |= QTextDocument::FindBackward;
  1231. }
  1232. textCursor = document->find(search, textCursor, flags);
  1233. if (textCursor.isNull()) {
  1234. // first search found nothing, wrap around and search again
  1235. textCursor = this->Output->textCursor();
  1236. textCursor.movePosition(directionForward ? QTextCursor::Start
  1237. : QTextCursor::End);
  1238. textCursor = document->find(search, textCursor, flags);
  1239. }
  1240. if (textCursor.hasSelection()) {
  1241. this->Output->setTextCursor(textCursor);
  1242. }
  1243. }
  1244. void CMakeSetupDialog::doOutputErrorNext()
  1245. {
  1246. QTextCursor textCursor = this->Output->textCursor();
  1247. bool atEnd = false;
  1248. // move cursor out of current error-block
  1249. if (textCursor.blockCharFormat() == this->ErrorFormat) {
  1250. atEnd = !textCursor.movePosition(QTextCursor::NextBlock);
  1251. }
  1252. // move cursor to next error-block
  1253. while (textCursor.blockCharFormat() != this->ErrorFormat && !atEnd) {
  1254. atEnd = !textCursor.movePosition(QTextCursor::NextBlock);
  1255. }
  1256. if (atEnd) {
  1257. // first search found nothing, wrap around and search again
  1258. atEnd = !textCursor.movePosition(QTextCursor::Start);
  1259. // move cursor to next error-block
  1260. while (textCursor.blockCharFormat() != this->ErrorFormat && !atEnd) {
  1261. atEnd = !textCursor.movePosition(QTextCursor::NextBlock);
  1262. }
  1263. }
  1264. if (!atEnd) {
  1265. textCursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
  1266. QTextCharFormat selectionFormat;
  1267. selectionFormat.setBackground(Qt::yellow);
  1268. QTextEdit::ExtraSelection extraSelection = { textCursor, selectionFormat };
  1269. this->Output->setExtraSelections(QList<QTextEdit::ExtraSelection>()
  1270. << extraSelection);
  1271. // make the whole error-block visible
  1272. this->Output->setTextCursor(textCursor);
  1273. // remove the selection to see the extraSelection
  1274. textCursor.setPosition(textCursor.anchor());
  1275. this->Output->setTextCursor(textCursor);
  1276. }
  1277. }
  1278. void CMakeSetupDialog::doWarningMessagesDialog()
  1279. {
  1280. WarningMessagesDialog dialog(this, this->CMakeThread->cmakeInstance());
  1281. dialog.exec();
  1282. }