CMakeSetupDialog.cxx 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  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.setArchitecture) {
  742. dialog.setPlatform(preset.architecture);
  743. }
  744. if (preset.setToolset) {
  745. dialog.setToolset(preset.toolset);
  746. }
  747. dialog.setCompilerOption(CompilerOption::DefaultNative);
  748. }
  749. if (dialog.exec() == QDialog::Accepted) {
  750. dialog.saveToSettings();
  751. this->CMakeThread->cmakeInstance()->setGenerator(dialog.getGenerator());
  752. this->CMakeThread->cmakeInstance()->setPlatform(dialog.getPlatform());
  753. this->CMakeThread->cmakeInstance()->setToolset(dialog.getToolset());
  754. QCMakeCacheModel* m = this->CacheValues->cacheModel();
  755. if (dialog.compilerSetup()) {
  756. QString fortranCompiler = dialog.getFortranCompiler();
  757. if (!fortranCompiler.isEmpty()) {
  758. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_Fortran_COMPILER",
  759. "Fortran compiler.", fortranCompiler, false);
  760. }
  761. QString cxxCompiler = dialog.getCXXCompiler();
  762. if (!cxxCompiler.isEmpty()) {
  763. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_CXX_COMPILER",
  764. "CXX compiler.", cxxCompiler, false);
  765. }
  766. QString cCompiler = dialog.getCCompiler();
  767. if (!cCompiler.isEmpty()) {
  768. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_C_COMPILER",
  769. "C compiler.", cCompiler, false);
  770. }
  771. } else if (dialog.crossCompilerSetup()) {
  772. QString fortranCompiler = dialog.getFortranCompiler();
  773. if (!fortranCompiler.isEmpty()) {
  774. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_Fortran_COMPILER",
  775. "Fortran compiler.", fortranCompiler, false);
  776. }
  777. QString mode = dialog.getCrossIncludeMode();
  778. m->insertProperty(QCMakeProperty::STRING,
  779. "CMAKE_FIND_ROOT_PATH_MODE_INCLUDE",
  780. tr("CMake Find Include Mode"), mode, false);
  781. mode = dialog.getCrossLibraryMode();
  782. m->insertProperty(QCMakeProperty::STRING,
  783. "CMAKE_FIND_ROOT_PATH_MODE_LIBRARY",
  784. tr("CMake Find Library Mode"), mode, false);
  785. mode = dialog.getCrossProgramMode();
  786. m->insertProperty(QCMakeProperty::STRING,
  787. "CMAKE_FIND_ROOT_PATH_MODE_PROGRAM",
  788. tr("CMake Find Program Mode"), mode, false);
  789. QString rootPath = dialog.getCrossRoot();
  790. m->insertProperty(QCMakeProperty::PATH, "CMAKE_FIND_ROOT_PATH",
  791. tr("CMake Find Root Path"), rootPath, false);
  792. QString systemName = dialog.getSystemName();
  793. m->insertProperty(QCMakeProperty::STRING, "CMAKE_SYSTEM_NAME",
  794. tr("CMake System Name"), systemName, false);
  795. QString systemVersion = dialog.getSystemVersion();
  796. m->insertProperty(QCMakeProperty::STRING, "CMAKE_SYSTEM_VERSION",
  797. tr("CMake System Version"), systemVersion, false);
  798. QString systemProcessor = dialog.getSystemProcessor();
  799. m->insertProperty(QCMakeProperty::STRING, "CMAKE_SYSTEM_PROCESSOR",
  800. tr("CMake System Processor"), systemProcessor, false);
  801. QString cxxCompiler = dialog.getCXXCompiler();
  802. if (!cxxCompiler.isEmpty()) {
  803. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_CXX_COMPILER",
  804. tr("CXX compiler."), cxxCompiler, false);
  805. }
  806. QString cCompiler = dialog.getCCompiler();
  807. if (!cCompiler.isEmpty()) {
  808. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_C_COMPILER",
  809. tr("C compiler."), cCompiler, false);
  810. }
  811. } else if (dialog.crossCompilerToolChainFile()) {
  812. QString toolchainFile = dialog.getCrossCompilerToolChainFile();
  813. m->insertProperty(QCMakeProperty::FILEPATH, "CMAKE_TOOLCHAIN_FILE",
  814. tr("Cross Compile ToolChain File"), toolchainFile,
  815. false);
  816. }
  817. return true;
  818. }
  819. return false;
  820. }
  821. void CMakeSetupDialog::updateGeneratorLabel(const QString& gen)
  822. {
  823. QString str = tr("Current Generator: ");
  824. if (gen.isEmpty()) {
  825. str += tr("None");
  826. } else {
  827. str += gen;
  828. }
  829. this->Generator->setText(str);
  830. }
  831. void CMakeSetupDialog::doReloadCache()
  832. {
  833. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(), "reloadCache",
  834. Qt::QueuedConnection);
  835. }
  836. void CMakeSetupDialog::doDeleteCache()
  837. {
  838. QString title = tr("Delete Cache");
  839. QString msg = tr("Are you sure you want to delete the cache?");
  840. QMessageBox::StandardButton btn;
  841. btn = QMessageBox::information(this, title, msg,
  842. QMessageBox::Yes | QMessageBox::No);
  843. if (btn == QMessageBox::No) {
  844. return;
  845. }
  846. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(), "deleteCache",
  847. Qt::QueuedConnection);
  848. }
  849. void CMakeSetupDialog::doAbout()
  850. {
  851. QString msg = tr(
  852. "CMake %1 (cmake.org).\n"
  853. "CMake suite maintained and supported by Kitware (kitware.com/cmake).\n"
  854. "Distributed under terms of the BSD 3-Clause License.\n"
  855. "\n"
  856. "CMake GUI maintained by csimsoft,\n"
  857. "built using Qt %2 (qt-project.org).\n"
  858. #ifdef USE_LGPL
  859. "\n"
  860. "The Qt Toolkit is Copyright (C) The Qt Company Ltd.\n"
  861. "Qt is licensed under terms of the GNU LGPLv" USE_LGPL ", available at:\n"
  862. " \"%3\""
  863. #endif
  864. );
  865. msg = msg.arg(cmVersion::GetCMakeVersion());
  866. msg = msg.arg(qVersion());
  867. #ifdef USE_LGPL
  868. std::string lgpl =
  869. cmSystemTools::GetCMakeRoot() + "/Licenses/LGPLv" USE_LGPL ".txt";
  870. msg = msg.arg(lgpl.c_str());
  871. #endif
  872. QDialog dialog;
  873. dialog.setWindowTitle(tr("About"));
  874. QVBoxLayout* l = new QVBoxLayout(&dialog);
  875. QLabel* lab = new QLabel(&dialog);
  876. l->addWidget(lab);
  877. lab->setText(msg);
  878. lab->setWordWrap(true);
  879. QDialogButtonBox* btns =
  880. new QDialogButtonBox(QDialogButtonBox::Ok, Qt::Horizontal, &dialog);
  881. QObject::connect(btns, &QDialogButtonBox::accepted, &dialog,
  882. &QDialog::accept);
  883. l->addWidget(btns);
  884. dialog.exec();
  885. }
  886. void CMakeSetupDialog::setExitAfterGenerate(bool b)
  887. {
  888. this->ExitAfterGenerate = b;
  889. }
  890. void CMakeSetupDialog::addBinaryPath(const QString& path)
  891. {
  892. QString cleanpath = QDir::cleanPath(path);
  893. // update UI
  894. this->BinaryDirectory->blockSignals(true);
  895. int idx = this->BinaryDirectory->findText(cleanpath);
  896. if (idx != -1) {
  897. this->BinaryDirectory->removeItem(idx);
  898. }
  899. this->BinaryDirectory->insertItem(0, cleanpath);
  900. this->BinaryDirectory->setCurrentIndex(0);
  901. this->BinaryDirectory->blockSignals(false);
  902. // save to registry
  903. QStringList buildPaths = this->loadBuildPaths();
  904. buildPaths.removeAll(cleanpath);
  905. buildPaths.prepend(cleanpath);
  906. this->saveBuildPaths(buildPaths);
  907. }
  908. void CMakeSetupDialog::dragEnterEvent(QDragEnterEvent* e)
  909. {
  910. if (!(this->CurrentState == ReadyConfigure ||
  911. this->CurrentState == ReadyGenerate)) {
  912. e->ignore();
  913. return;
  914. }
  915. const QMimeData* dat = e->mimeData();
  916. QList<QUrl> urls = dat->urls();
  917. QString file = urls.count() ? urls[0].toLocalFile() : QString();
  918. if (!file.isEmpty() &&
  919. (file.endsWith("CMakeCache.txt", Qt::CaseInsensitive) ||
  920. file.endsWith("CMakeLists.txt", Qt::CaseInsensitive))) {
  921. e->accept();
  922. } else {
  923. e->ignore();
  924. }
  925. }
  926. void CMakeSetupDialog::dropEvent(QDropEvent* e)
  927. {
  928. if (!(this->CurrentState == ReadyConfigure ||
  929. this->CurrentState == ReadyGenerate)) {
  930. return;
  931. }
  932. const QMimeData* dat = e->mimeData();
  933. QList<QUrl> urls = dat->urls();
  934. QString file = urls.count() ? urls[0].toLocalFile() : QString();
  935. if (file.endsWith("CMakeCache.txt", Qt::CaseInsensitive)) {
  936. QFileInfo info(file);
  937. if (this->CMakeThread->cmakeInstance()->binaryDirectory() !=
  938. info.absolutePath()) {
  939. this->setBinaryDirectory(info.absolutePath());
  940. }
  941. } else if (file.endsWith("CMakeLists.txt", Qt::CaseInsensitive)) {
  942. QFileInfo info(file);
  943. if (this->CMakeThread->cmakeInstance()->binaryDirectory() !=
  944. info.absolutePath()) {
  945. this->setSourceDirectory(info.absolutePath());
  946. this->setBinaryDirectory(info.absolutePath());
  947. }
  948. }
  949. }
  950. QStringList CMakeSetupDialog::loadBuildPaths()
  951. {
  952. QSettings settings;
  953. settings.beginGroup("Settings/StartPath");
  954. QStringList buildPaths;
  955. for (int i = 0; i < 10; i++) {
  956. QString p = settings.value(QString("WhereBuild%1").arg(i)).toString();
  957. if (!p.isEmpty()) {
  958. buildPaths.append(p);
  959. }
  960. }
  961. return buildPaths;
  962. }
  963. void CMakeSetupDialog::saveBuildPaths(const QStringList& paths)
  964. {
  965. QSettings settings;
  966. settings.beginGroup("Settings/StartPath");
  967. int num = paths.count();
  968. if (num > 10) {
  969. num = 10;
  970. }
  971. for (int i = 0; i < num; i++) {
  972. settings.setValue(QString("WhereBuild%1").arg(i), paths[i]);
  973. }
  974. }
  975. void CMakeSetupDialog::setCacheModified()
  976. {
  977. this->CacheModified = true;
  978. this->ConfigureNeeded = true;
  979. this->enterState(ReadyConfigure);
  980. }
  981. void CMakeSetupDialog::removeSelectedCacheEntries()
  982. {
  983. QModelIndexList idxs = this->CacheValues->selectionModel()->selectedRows();
  984. QList<QPersistentModelIndex> pidxs;
  985. foreach (QModelIndex const& i, idxs) {
  986. pidxs.append(i);
  987. }
  988. foreach (QPersistentModelIndex const& pi, pidxs) {
  989. this->CacheValues->model()->removeRow(pi.row(), pi.parent());
  990. }
  991. }
  992. void CMakeSetupDialog::selectionChanged()
  993. {
  994. QModelIndexList idxs = this->CacheValues->selectionModel()->selectedRows();
  995. if (idxs.count() &&
  996. (this->CurrentState == ReadyConfigure ||
  997. this->CurrentState == ReadyGenerate)) {
  998. this->RemoveEntry->setEnabled(true);
  999. } else {
  1000. this->RemoveEntry->setEnabled(false);
  1001. }
  1002. }
  1003. void CMakeSetupDialog::enterState(CMakeSetupDialog::State s)
  1004. {
  1005. if (s == this->CurrentState) {
  1006. return;
  1007. }
  1008. this->CurrentState = s;
  1009. if (s == Interrupting) {
  1010. this->ConfigureButton->setEnabled(false);
  1011. this->GenerateButton->setEnabled(false);
  1012. this->OpenProjectButton->setEnabled(false);
  1013. } else if (s == Configuring) {
  1014. this->setEnabledState(false);
  1015. this->GenerateButton->setEnabled(false);
  1016. this->GenerateAction->setEnabled(false);
  1017. this->OpenProjectButton->setEnabled(false);
  1018. this->ConfigureButton->setText(tr("&Stop"));
  1019. } else if (s == Generating) {
  1020. this->CacheModified = false;
  1021. this->setEnabledState(false);
  1022. this->ConfigureButton->setEnabled(false);
  1023. this->GenerateAction->setEnabled(false);
  1024. this->OpenProjectButton->setEnabled(false);
  1025. this->GenerateButton->setText(tr("&Stop"));
  1026. } else if (s == ReadyConfigure || s == ReadyGenerate) {
  1027. this->setEnabledState(true);
  1028. this->GenerateButton->setEnabled(true);
  1029. this->GenerateAction->setEnabled(true);
  1030. this->ConfigureButton->setEnabled(true);
  1031. this->ConfigureButton->setText(tr("&Configure"));
  1032. this->GenerateButton->setText(tr("&Generate"));
  1033. }
  1034. }
  1035. void CMakeSetupDialog::editEnvironment()
  1036. {
  1037. EnvironmentDialog dialog(this->CMakeThread->cmakeInstance()->environment(),
  1038. this);
  1039. if (dialog.exec() == QDialog::Accepted) {
  1040. QMetaObject::invokeMethod(
  1041. this->CMakeThread->cmakeInstance(), "setEnvironment",
  1042. Q_ARG(QProcessEnvironment, dialog.environment()));
  1043. }
  1044. }
  1045. void CMakeSetupDialog::addCacheEntry()
  1046. {
  1047. QDialog dialog(this);
  1048. dialog.resize(400, 200);
  1049. dialog.setWindowTitle(tr("Add Cache Entry"));
  1050. QVBoxLayout* l = new QVBoxLayout(&dialog);
  1051. AddCacheEntry* w =
  1052. new AddCacheEntry(&dialog, this->AddVariableNames, this->AddVariableTypes);
  1053. QDialogButtonBox* btns = new QDialogButtonBox(
  1054. QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &dialog);
  1055. QObject::connect(btns, &QDialogButtonBox::accepted, &dialog,
  1056. &QDialog::accept);
  1057. QObject::connect(btns, &QDialogButtonBox::rejected, &dialog,
  1058. &QDialog::reject);
  1059. l->addWidget(w);
  1060. l->addStretch();
  1061. l->addWidget(btns);
  1062. if (QDialog::Accepted == dialog.exec()) {
  1063. QCMakeCacheModel* m = this->CacheValues->cacheModel();
  1064. m->insertProperty(w->type(), w->name(), w->description(), w->value(),
  1065. false);
  1066. // only add variable names to the completion which are new
  1067. if (!this->AddVariableNames.contains(w->name())) {
  1068. this->AddVariableNames << w->name();
  1069. this->AddVariableTypes << w->typeString();
  1070. // limit to at most 100 completion items
  1071. if (this->AddVariableNames.size() > 100) {
  1072. this->AddVariableNames.removeFirst();
  1073. this->AddVariableTypes.removeFirst();
  1074. }
  1075. // make sure CMAKE_INSTALL_PREFIX is always there
  1076. if (!this->AddVariableNames.contains("CMAKE_INSTALL_PREFIX")) {
  1077. this->AddVariableNames << "CMAKE_INSTALL_PREFIX";
  1078. this->AddVariableTypes << "PATH";
  1079. }
  1080. QSettings settings;
  1081. settings.beginGroup("Settings/StartPath");
  1082. settings.setValue("AddVariableNames", this->AddVariableNames);
  1083. settings.setValue("AddVariableTypes", this->AddVariableTypes);
  1084. }
  1085. }
  1086. }
  1087. void CMakeSetupDialog::startSearch()
  1088. {
  1089. this->Search->setFocus(Qt::OtherFocusReason);
  1090. this->Search->selectAll();
  1091. }
  1092. void CMakeSetupDialog::setDebugOutput(bool flag)
  1093. {
  1094. QMetaObject::invokeMethod(this->CMakeThread->cmakeInstance(),
  1095. "setDebugOutput", Qt::QueuedConnection,
  1096. Q_ARG(bool, flag));
  1097. }
  1098. void CMakeSetupDialog::setGroupedView(bool v)
  1099. {
  1100. this->CacheValues->cacheModel()->setViewType(v ? QCMakeCacheModel::GroupView
  1101. : QCMakeCacheModel::FlatView);
  1102. this->CacheValues->setRootIsDecorated(v);
  1103. QSettings settings;
  1104. settings.beginGroup("Settings/StartPath");
  1105. settings.setValue("GroupView", v);
  1106. }
  1107. void CMakeSetupDialog::setAdvancedView(bool v)
  1108. {
  1109. this->CacheValues->setShowAdvanced(v);
  1110. QSettings settings;
  1111. settings.beginGroup("Settings/StartPath");
  1112. settings.setValue("AdvancedView", v);
  1113. }
  1114. void CMakeSetupDialog::showUserChanges()
  1115. {
  1116. QSet<QCMakeProperty> changes =
  1117. qobject_cast<QCMakeCacheModelDelegate*>(this->CacheValues->itemDelegate())
  1118. ->changes();
  1119. QDialog dialog(this);
  1120. dialog.setWindowTitle(tr("My Changes"));
  1121. dialog.resize(600, 400);
  1122. QVBoxLayout* l = new QVBoxLayout(&dialog);
  1123. QTextEdit* textedit = new QTextEdit(&dialog);
  1124. textedit->setReadOnly(true);
  1125. l->addWidget(textedit);
  1126. QDialogButtonBox* btns =
  1127. new QDialogButtonBox(QDialogButtonBox::Close, Qt::Horizontal, &dialog);
  1128. QObject::connect(btns, &QDialogButtonBox::rejected, &dialog,
  1129. &QDialog::accept);
  1130. l->addWidget(btns);
  1131. QString command;
  1132. QString cache;
  1133. foreach (QCMakeProperty const& prop, changes) {
  1134. QString type;
  1135. switch (prop.Type) {
  1136. case QCMakeProperty::BOOL:
  1137. type = "BOOL";
  1138. break;
  1139. case QCMakeProperty::PATH:
  1140. type = "PATH";
  1141. break;
  1142. case QCMakeProperty::FILEPATH:
  1143. type = "FILEPATH";
  1144. break;
  1145. case QCMakeProperty::STRING:
  1146. type = "STRING";
  1147. break;
  1148. }
  1149. QString value;
  1150. if (prop.Type == QCMakeProperty::BOOL) {
  1151. value = prop.Value.toBool() ? "1" : "0";
  1152. } else {
  1153. value = prop.Value.toString();
  1154. }
  1155. QString const line = QString("%1:%2=").arg(prop.Key, type);
  1156. command += QString("-D%1\"%2\" ").arg(line, value);
  1157. cache += QString("%1%2\n").arg(line, value);
  1158. }
  1159. textedit->append(tr("Commandline options:"));
  1160. textedit->append(command);
  1161. textedit->append("\n");
  1162. textedit->append(tr("Cache file:"));
  1163. textedit->append(cache);
  1164. dialog.exec();
  1165. }
  1166. void CMakeSetupDialog::setSearchFilter(const QString& str)
  1167. {
  1168. this->CacheValues->selectionModel()->clear();
  1169. this->CacheValues->setSearchFilter(str);
  1170. }
  1171. void CMakeSetupDialog::doOutputContextMenu(QPoint pt)
  1172. {
  1173. std::unique_ptr<QMenu> menu(this->Output->createStandardContextMenu());
  1174. menu->addSeparator();
  1175. auto* a = menu->addAction(tr("Find..."));
  1176. QObject::connect(a, &QAction::triggered, this,
  1177. &CMakeSetupDialog::doOutputFindDialog);
  1178. a->setShortcut(QKeySequence::Find);
  1179. a = menu->addAction(tr("Find Next"));
  1180. QObject::connect(a, &QAction::triggered, this,
  1181. &CMakeSetupDialog::doOutputFindNext);
  1182. a->setShortcut(QKeySequence::FindNext);
  1183. a = menu->addAction(tr("Find Previous"));
  1184. QObject::connect(a, &QAction::triggered, this,
  1185. &CMakeSetupDialog::doOutputFindPrev);
  1186. a->setShortcut(QKeySequence::FindPrevious);
  1187. menu->addSeparator();
  1188. a = menu->addAction(tr("Goto Next Error"));
  1189. QObject::connect(a, &QAction::triggered, this,
  1190. &CMakeSetupDialog::doOutputErrorNext);
  1191. a->setShortcut(QKeySequence(Qt::Key_F8));
  1192. menu->exec(this->Output->mapToGlobal(pt));
  1193. }
  1194. void CMakeSetupDialog::doOutputFindDialog()
  1195. {
  1196. QStringList strings(this->FindHistory);
  1197. QString selection = this->Output->textCursor().selectedText();
  1198. if (!selection.isEmpty() && !selection.contains(QChar::ParagraphSeparator) &&
  1199. !selection.contains(QChar::LineSeparator)) {
  1200. strings.push_front(selection);
  1201. }
  1202. bool ok;
  1203. QString search = QInputDialog::getItem(this, tr("Find in Output"),
  1204. tr("Find:"), strings, 0, true, &ok);
  1205. if (ok && !search.isEmpty()) {
  1206. if (!this->FindHistory.contains(search)) {
  1207. this->FindHistory.push_front(search);
  1208. }
  1209. doOutputFindNext();
  1210. }
  1211. }
  1212. void CMakeSetupDialog::doRegexExplorerDialog()
  1213. {
  1214. RegexExplorer dialog(this);
  1215. dialog.exec();
  1216. }
  1217. void CMakeSetupDialog::doOutputFindPrev()
  1218. {
  1219. doOutputFindNext(false);
  1220. }
  1221. void CMakeSetupDialog::doOutputFindNext(bool directionForward)
  1222. {
  1223. if (this->FindHistory.isEmpty()) {
  1224. doOutputFindDialog(); // will re-call this function again
  1225. return;
  1226. }
  1227. QString search = this->FindHistory.front();
  1228. QTextCursor textCursor = this->Output->textCursor();
  1229. QTextDocument* document = this->Output->document();
  1230. QTextDocument::FindFlags flags;
  1231. if (!directionForward) {
  1232. flags |= QTextDocument::FindBackward;
  1233. }
  1234. textCursor = document->find(search, textCursor, flags);
  1235. if (textCursor.isNull()) {
  1236. // first search found nothing, wrap around and search again
  1237. textCursor = this->Output->textCursor();
  1238. textCursor.movePosition(directionForward ? QTextCursor::Start
  1239. : QTextCursor::End);
  1240. textCursor = document->find(search, textCursor, flags);
  1241. }
  1242. if (textCursor.hasSelection()) {
  1243. this->Output->setTextCursor(textCursor);
  1244. }
  1245. }
  1246. void CMakeSetupDialog::doOutputErrorNext()
  1247. {
  1248. QTextCursor textCursor = this->Output->textCursor();
  1249. bool atEnd = false;
  1250. // move cursor out of current error-block
  1251. if (textCursor.blockCharFormat() == this->ErrorFormat) {
  1252. atEnd = !textCursor.movePosition(QTextCursor::NextBlock);
  1253. }
  1254. // move cursor to next error-block
  1255. while (textCursor.blockCharFormat() != this->ErrorFormat && !atEnd) {
  1256. atEnd = !textCursor.movePosition(QTextCursor::NextBlock);
  1257. }
  1258. if (atEnd) {
  1259. // first search found nothing, wrap around and search again
  1260. atEnd = !textCursor.movePosition(QTextCursor::Start);
  1261. // move cursor to next error-block
  1262. while (textCursor.blockCharFormat() != this->ErrorFormat && !atEnd) {
  1263. atEnd = !textCursor.movePosition(QTextCursor::NextBlock);
  1264. }
  1265. }
  1266. if (!atEnd) {
  1267. textCursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
  1268. QTextCharFormat selectionFormat;
  1269. selectionFormat.setBackground(Qt::yellow);
  1270. QTextEdit::ExtraSelection extraSelection = { textCursor, selectionFormat };
  1271. this->Output->setExtraSelections(QList<QTextEdit::ExtraSelection>()
  1272. << extraSelection);
  1273. // make the whole error-block visible
  1274. this->Output->setTextCursor(textCursor);
  1275. // remove the selection to see the extraSelection
  1276. textCursor.setPosition(textCursor.anchor());
  1277. this->Output->setTextCursor(textCursor);
  1278. }
  1279. }
  1280. void CMakeSetupDialog::doWarningMessagesDialog()
  1281. {
  1282. WarningMessagesDialog dialog(this, this->CMakeThread->cmakeInstance());
  1283. dialog.exec();
  1284. }