CMakeSetupDialog.cxx 49 KB

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