CMakeSetupDialog.cxx 49 KB

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