qconf.cc 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
  4. * Copyright (C) 2015 Boris Barbulovski <bbarbulovski@gmail.com>
  5. */
  6. #include <QAction>
  7. #include <QActionGroup>
  8. #include <QApplication>
  9. #include <QCloseEvent>
  10. #include <QDebug>
  11. #include <QFileDialog>
  12. #include <QLabel>
  13. #include <QLayout>
  14. #include <QList>
  15. #include <QMenu>
  16. #include <QMenuBar>
  17. #include <QMessageBox>
  18. #include <QRegularExpression>
  19. #include <QScreen>
  20. #include <QToolBar>
  21. #include <stdlib.h>
  22. #include <xalloc.h>
  23. #include "lkc.h"
  24. #include "qconf.h"
  25. static QApplication *configApp;
  26. static ConfigSettings *configSettings;
  27. QAction *ConfigMainWindow::saveAction;
  28. ConfigSettings::ConfigSettings()
  29. : QSettings("kernel.org", "qconf")
  30. {
  31. beginGroup("/kconfig/qconf");
  32. }
  33. ConfigSettings::~ConfigSettings()
  34. {
  35. endGroup();
  36. }
  37. /**
  38. * Reads a list of integer values from the application settings.
  39. */
  40. QList<int> ConfigSettings::readSizes(const QString& key, bool *ok)
  41. {
  42. QList<int> result;
  43. if (contains(key))
  44. {
  45. QStringList entryList = value(key).toStringList();
  46. QStringList::Iterator it;
  47. for (it = entryList.begin(); it != entryList.end(); ++it)
  48. result.push_back((*it).toInt());
  49. *ok = true;
  50. }
  51. else
  52. *ok = false;
  53. return result;
  54. }
  55. /**
  56. * Writes a list of integer values to the application settings.
  57. */
  58. bool ConfigSettings::writeSizes(const QString& key, const QList<int>& value)
  59. {
  60. QStringList stringList;
  61. QList<int>::ConstIterator it;
  62. for (it = value.begin(); it != value.end(); ++it)
  63. stringList.push_back(QString::number(*it));
  64. setValue(key, stringList);
  65. return true;
  66. }
  67. QIcon ConfigItem::symbolYesIcon;
  68. QIcon ConfigItem::symbolModIcon;
  69. QIcon ConfigItem::symbolNoIcon;
  70. QIcon ConfigItem::choiceYesIcon;
  71. QIcon ConfigItem::choiceNoIcon;
  72. QIcon ConfigItem::menuIcon;
  73. QIcon ConfigItem::menubackIcon;
  74. /*
  75. * update the displayed of a menu entry
  76. */
  77. void ConfigItem::updateMenu(void)
  78. {
  79. ConfigList* list;
  80. struct symbol* sym;
  81. QString prompt;
  82. int type;
  83. tristate expr;
  84. list = listView();
  85. if (goParent) {
  86. setIcon(promptColIdx, menubackIcon);
  87. prompt = "..";
  88. goto set_prompt;
  89. }
  90. sym = menu->sym;
  91. prompt = menu_get_prompt(menu);
  92. switch (menu->type) {
  93. case M_MENU:
  94. if (list->mode == singleMode) {
  95. /* a menuconfig entry is displayed differently
  96. * depending whether it's at the view root or a child.
  97. */
  98. if (sym && list->rootEntry == menu)
  99. break;
  100. setIcon(promptColIdx, menuIcon);
  101. } else {
  102. if (sym)
  103. break;
  104. setIcon(promptColIdx, QIcon());
  105. }
  106. goto set_prompt;
  107. case M_COMMENT:
  108. setIcon(promptColIdx, QIcon());
  109. prompt = "*** " + prompt + " ***";
  110. goto set_prompt;
  111. case M_CHOICE:
  112. setIcon(promptColIdx, QIcon());
  113. sym = sym_calc_choice(menu);
  114. if (sym)
  115. setText(dataColIdx, sym->name);
  116. goto set_prompt;
  117. default:
  118. ;
  119. }
  120. if (!sym)
  121. goto set_prompt;
  122. setText(nameColIdx, sym->name);
  123. type = sym_get_type(sym);
  124. switch (type) {
  125. case S_BOOLEAN:
  126. case S_TRISTATE:
  127. char ch;
  128. if (!sym_is_changeable(sym) && list->optMode == normalOpt) {
  129. setIcon(promptColIdx, QIcon());
  130. break;
  131. }
  132. expr = sym_get_tristate_value(sym);
  133. switch (expr) {
  134. case yes:
  135. if (sym_is_choice_value(sym))
  136. setIcon(promptColIdx, choiceYesIcon);
  137. else
  138. setIcon(promptColIdx, symbolYesIcon);
  139. ch = 'Y';
  140. break;
  141. case mod:
  142. setIcon(promptColIdx, symbolModIcon);
  143. ch = 'M';
  144. break;
  145. default:
  146. if (sym_is_choice_value(sym))
  147. setIcon(promptColIdx, choiceNoIcon);
  148. else
  149. setIcon(promptColIdx, symbolNoIcon);
  150. ch = 'N';
  151. break;
  152. }
  153. setText(dataColIdx, QChar(ch));
  154. break;
  155. case S_INT:
  156. case S_HEX:
  157. case S_STRING:
  158. setText(dataColIdx, sym_get_string_value(sym));
  159. break;
  160. }
  161. if (!sym_has_value(sym))
  162. prompt += " (NEW)";
  163. set_prompt:
  164. setText(promptColIdx, prompt);
  165. }
  166. void ConfigItem::testUpdateMenu(void)
  167. {
  168. ConfigItem* i;
  169. if (!menu)
  170. return;
  171. if (menu->type == M_CHOICE)
  172. sym_calc_choice(menu);
  173. else
  174. sym_calc_value(menu->sym);
  175. if (menu->flags & MENU_CHANGED) {
  176. /* the menu entry changed, so update all list items */
  177. menu->flags &= ~MENU_CHANGED;
  178. for (i = (ConfigItem*)menu->data; i; i = i->nextItem)
  179. i->updateMenu();
  180. } else if (listView()->updateAll)
  181. updateMenu();
  182. }
  183. /*
  184. * construct a menu entry
  185. */
  186. void ConfigItem::init(void)
  187. {
  188. if (menu) {
  189. ConfigList* list = listView();
  190. nextItem = (ConfigItem*)menu->data;
  191. menu->data = this;
  192. if (list->mode != fullMode)
  193. setExpanded(true);
  194. sym_calc_value(menu->sym);
  195. if (menu->sym) {
  196. enum symbol_type type = menu->sym->type;
  197. // Allow to edit "int", "hex", and "string" in-place in
  198. // the data column. Unfortunately, you cannot specify
  199. // the flags per column. Set ItemIsEditable for all
  200. // columns here, and check the column in createEditor().
  201. if (type == S_INT || type == S_HEX || type == S_STRING)
  202. setFlags(flags() | Qt::ItemIsEditable);
  203. }
  204. }
  205. updateMenu();
  206. }
  207. /*
  208. * destruct a menu entry
  209. */
  210. ConfigItem::~ConfigItem(void)
  211. {
  212. if (menu) {
  213. ConfigItem** ip = (ConfigItem**)&menu->data;
  214. for (; *ip; ip = &(*ip)->nextItem) {
  215. if (*ip == this) {
  216. *ip = nextItem;
  217. break;
  218. }
  219. }
  220. }
  221. }
  222. QWidget *ConfigItemDelegate::createEditor(QWidget *parent,
  223. const QStyleOptionViewItem &option,
  224. const QModelIndex &index) const
  225. {
  226. ConfigItem *item;
  227. // Only the data column is editable
  228. if (index.column() != dataColIdx)
  229. return nullptr;
  230. // You cannot edit invisible menus
  231. item = static_cast<ConfigItem *>(index.internalPointer());
  232. if (!item || !item->menu || !menu_is_visible(item->menu))
  233. return nullptr;
  234. return QStyledItemDelegate::createEditor(parent, option, index);
  235. }
  236. void ConfigItemDelegate::setModelData(QWidget *editor,
  237. QAbstractItemModel *model,
  238. const QModelIndex &index) const
  239. {
  240. QLineEdit *lineEdit;
  241. ConfigItem *item;
  242. struct symbol *sym;
  243. bool success;
  244. lineEdit = qobject_cast<QLineEdit *>(editor);
  245. // If this is not a QLineEdit, use the parent's default.
  246. // (does this happen?)
  247. if (!lineEdit)
  248. goto parent;
  249. item = static_cast<ConfigItem *>(index.internalPointer());
  250. if (!item || !item->menu)
  251. goto parent;
  252. sym = item->menu->sym;
  253. if (!sym)
  254. goto parent;
  255. success = sym_set_string_value(sym, lineEdit->text().toUtf8().data());
  256. if (success) {
  257. ConfigList::updateListForAll();
  258. } else {
  259. QMessageBox::information(editor, "qconf",
  260. "Cannot set the data (maybe due to out of range).\n"
  261. "Setting the old value.");
  262. lineEdit->setText(sym_get_string_value(sym));
  263. }
  264. parent:
  265. QStyledItemDelegate::setModelData(editor, model, index);
  266. }
  267. ConfigList::ConfigList(QWidget *parent, const char *name)
  268. : QTreeWidget(parent),
  269. updateAll(false),
  270. showName(false), mode(singleMode), optMode(normalOpt),
  271. rootEntry(0), headerPopup(0)
  272. {
  273. setObjectName(name);
  274. setSortingEnabled(false);
  275. setVerticalScrollMode(ScrollPerPixel);
  276. setHorizontalScrollMode(ScrollPerPixel);
  277. setHeaderLabels(QStringList() << "Option" << "Name" << "Value");
  278. connect(this, &ConfigList::itemSelectionChanged,
  279. this, &ConfigList::updateSelection);
  280. if (name) {
  281. configSettings->beginGroup(name);
  282. showName = configSettings->value("/showName", false).toBool();
  283. optMode = (enum optionMode)configSettings->value("/optionMode", 0).toInt();
  284. configSettings->endGroup();
  285. connect(configApp, &QApplication::aboutToQuit,
  286. this, &ConfigList::saveSettings);
  287. }
  288. showColumn(promptColIdx);
  289. setItemDelegate(new ConfigItemDelegate(this));
  290. allLists.append(this);
  291. reinit();
  292. }
  293. ConfigList::~ConfigList()
  294. {
  295. allLists.removeOne(this);
  296. }
  297. bool ConfigList::menuSkip(struct menu *menu)
  298. {
  299. if (optMode == normalOpt && menu_is_visible(menu))
  300. return false;
  301. if (optMode == promptOpt && menu_has_prompt(menu))
  302. return false;
  303. if (optMode == allOpt)
  304. return false;
  305. return true;
  306. }
  307. void ConfigList::reinit(void)
  308. {
  309. hideColumn(nameColIdx);
  310. if (showName)
  311. showColumn(nameColIdx);
  312. updateListAll();
  313. }
  314. void ConfigList::setOptionMode(QAction *action)
  315. {
  316. if (action == showNormalAction)
  317. optMode = normalOpt;
  318. else if (action == showAllAction)
  319. optMode = allOpt;
  320. else
  321. optMode = promptOpt;
  322. updateListAll();
  323. }
  324. void ConfigList::saveSettings(void)
  325. {
  326. if (!objectName().isEmpty()) {
  327. configSettings->beginGroup(objectName());
  328. configSettings->setValue("/showName", showName);
  329. configSettings->setValue("/optionMode", (int)optMode);
  330. configSettings->endGroup();
  331. }
  332. }
  333. ConfigItem* ConfigList::findConfigItem(struct menu *menu)
  334. {
  335. ConfigItem* item = (ConfigItem*)menu->data;
  336. for (; item; item = item->nextItem) {
  337. if (this == item->listView())
  338. break;
  339. }
  340. return item;
  341. }
  342. void ConfigList::updateSelection(void)
  343. {
  344. struct menu *menu;
  345. enum prop_type type;
  346. if (selectedItems().count() == 0)
  347. return;
  348. ConfigItem* item = (ConfigItem*)selectedItems().first();
  349. if (!item)
  350. return;
  351. menu = item->menu;
  352. emit menuChanged(menu);
  353. if (!menu)
  354. return;
  355. type = menu->prompt ? menu->prompt->type : P_UNKNOWN;
  356. if (mode == menuMode && type == P_MENU)
  357. emit menuSelected(menu);
  358. }
  359. void ConfigList::updateList()
  360. {
  361. ConfigItem* last = 0;
  362. ConfigItem *item;
  363. if (!rootEntry) {
  364. if (mode != listMode)
  365. goto update;
  366. QTreeWidgetItemIterator it(this);
  367. while (*it) {
  368. item = (ConfigItem*)(*it);
  369. if (!item->menu)
  370. continue;
  371. item->testUpdateMenu();
  372. ++it;
  373. }
  374. return;
  375. }
  376. if (rootEntry != &rootmenu && mode == singleMode) {
  377. item = (ConfigItem *)topLevelItem(0);
  378. if (!item)
  379. item = new ConfigItem(this, 0);
  380. last = item;
  381. }
  382. if ((mode == singleMode || (mode == symbolMode && !(rootEntry->flags & MENU_ROOT))) &&
  383. rootEntry->sym && rootEntry->prompt) {
  384. item = last ? last->nextSibling() : nullptr;
  385. if (!item)
  386. item = new ConfigItem(this, last, rootEntry);
  387. else
  388. item->testUpdateMenu();
  389. updateMenuList(item, rootEntry);
  390. update();
  391. resizeColumnToContents(0);
  392. return;
  393. }
  394. update:
  395. updateMenuList(rootEntry);
  396. update();
  397. resizeColumnToContents(0);
  398. }
  399. void ConfigList::updateListForAll()
  400. {
  401. QListIterator<ConfigList *> it(allLists);
  402. while (it.hasNext()) {
  403. ConfigList *list = it.next();
  404. list->updateList();
  405. }
  406. }
  407. void ConfigList::updateListAllForAll()
  408. {
  409. QListIterator<ConfigList *> it(allLists);
  410. while (it.hasNext()) {
  411. ConfigList *list = it.next();
  412. list->updateListAll();
  413. }
  414. }
  415. void ConfigList::setValue(ConfigItem* item, tristate val)
  416. {
  417. struct symbol* sym;
  418. int type;
  419. tristate oldval;
  420. sym = item->menu ? item->menu->sym : 0;
  421. if (!sym)
  422. return;
  423. type = sym_get_type(sym);
  424. switch (type) {
  425. case S_BOOLEAN:
  426. case S_TRISTATE:
  427. oldval = sym_get_tristate_value(sym);
  428. if (!sym_set_tristate_value(sym, val))
  429. return;
  430. if (oldval == no && item->menu->list)
  431. item->setExpanded(true);
  432. ConfigList::updateListForAll();
  433. break;
  434. }
  435. }
  436. void ConfigList::changeValue(ConfigItem* item)
  437. {
  438. struct symbol* sym;
  439. struct menu* menu;
  440. int type, oldexpr, newexpr;
  441. menu = item->menu;
  442. if (!menu)
  443. return;
  444. sym = menu->sym;
  445. if (!sym) {
  446. if (item->menu->list)
  447. item->setExpanded(!item->isExpanded());
  448. return;
  449. }
  450. type = sym_get_type(sym);
  451. switch (type) {
  452. case S_BOOLEAN:
  453. case S_TRISTATE:
  454. oldexpr = sym_get_tristate_value(sym);
  455. newexpr = sym_toggle_tristate_value(sym);
  456. if (item->menu->list) {
  457. if (oldexpr == newexpr)
  458. item->setExpanded(!item->isExpanded());
  459. else if (oldexpr == no)
  460. item->setExpanded(true);
  461. }
  462. if (oldexpr != newexpr)
  463. ConfigList::updateListForAll();
  464. break;
  465. default:
  466. break;
  467. }
  468. }
  469. void ConfigList::setRootMenu(struct menu *menu)
  470. {
  471. enum prop_type type;
  472. if (rootEntry == menu)
  473. return;
  474. type = menu && menu->prompt ? menu->prompt->type : P_UNKNOWN;
  475. if (type != P_MENU)
  476. return;
  477. updateMenuList(0);
  478. rootEntry = menu;
  479. updateListAll();
  480. if (currentItem()) {
  481. setSelected(currentItem(), hasFocus());
  482. scrollToItem(currentItem());
  483. }
  484. }
  485. void ConfigList::setParentMenu(void)
  486. {
  487. ConfigItem* item;
  488. struct menu *oldroot;
  489. oldroot = rootEntry;
  490. if (rootEntry == &rootmenu)
  491. return;
  492. setRootMenu(menu_get_menu_or_parent_menu(rootEntry->parent));
  493. QTreeWidgetItemIterator it(this);
  494. while (*it) {
  495. item = (ConfigItem *)(*it);
  496. if (item->menu == oldroot) {
  497. setCurrentItem(item);
  498. scrollToItem(item);
  499. break;
  500. }
  501. ++it;
  502. }
  503. }
  504. /*
  505. * update all the children of a menu entry
  506. * removes/adds the entries from the parent widget as necessary
  507. *
  508. * parent: either the menu list widget or a menu entry widget
  509. * menu: entry to be updated
  510. */
  511. void ConfigList::updateMenuList(ConfigItem *parent, struct menu* menu)
  512. {
  513. struct menu* child;
  514. ConfigItem* item;
  515. ConfigItem* last;
  516. enum prop_type type;
  517. if (!menu) {
  518. while (parent->childCount() > 0)
  519. {
  520. delete parent->takeChild(0);
  521. }
  522. return;
  523. }
  524. last = parent->firstChild();
  525. if (last && !last->goParent)
  526. last = 0;
  527. for (child = menu->list; child; child = child->next) {
  528. item = last ? last->nextSibling() : parent->firstChild();
  529. type = child->prompt ? child->prompt->type : P_UNKNOWN;
  530. switch (mode) {
  531. case menuMode:
  532. if (!(child->flags & MENU_ROOT))
  533. goto hide;
  534. break;
  535. case symbolMode:
  536. if (child->flags & MENU_ROOT)
  537. goto hide;
  538. break;
  539. default:
  540. break;
  541. }
  542. if (!menuSkip(child)) {
  543. if (!child->sym && !child->list && !child->prompt)
  544. continue;
  545. if (!item || item->menu != child)
  546. item = new ConfigItem(parent, last, child);
  547. else
  548. item->testUpdateMenu();
  549. if (mode == fullMode || mode == menuMode || type != P_MENU)
  550. updateMenuList(item, child);
  551. else
  552. updateMenuList(item, 0);
  553. last = item;
  554. continue;
  555. }
  556. hide:
  557. if (item && item->menu == child) {
  558. last = parent->firstChild();
  559. if (last == item)
  560. last = 0;
  561. else while (last->nextSibling() != item)
  562. last = last->nextSibling();
  563. delete item;
  564. }
  565. }
  566. }
  567. void ConfigList::updateMenuList(struct menu *menu)
  568. {
  569. struct menu* child;
  570. ConfigItem* item;
  571. ConfigItem* last;
  572. enum prop_type type;
  573. if (!menu) {
  574. while (topLevelItemCount() > 0)
  575. {
  576. delete takeTopLevelItem(0);
  577. }
  578. return;
  579. }
  580. last = (ConfigItem *)topLevelItem(0);
  581. if (last && !last->goParent)
  582. last = 0;
  583. for (child = menu->list; child; child = child->next) {
  584. item = last ? last->nextSibling() : (ConfigItem *)topLevelItem(0);
  585. type = child->prompt ? child->prompt->type : P_UNKNOWN;
  586. switch (mode) {
  587. case menuMode:
  588. if (!(child->flags & MENU_ROOT))
  589. goto hide;
  590. break;
  591. case symbolMode:
  592. if (child->flags & MENU_ROOT)
  593. goto hide;
  594. break;
  595. default:
  596. break;
  597. }
  598. if (!menuSkip(child)) {
  599. if (!child->sym && !child->list && !child->prompt)
  600. continue;
  601. if (!item || item->menu != child)
  602. item = new ConfigItem(this, last, child);
  603. else
  604. item->testUpdateMenu();
  605. if (mode == fullMode || mode == menuMode || type != P_MENU)
  606. updateMenuList(item, child);
  607. else
  608. updateMenuList(item, 0);
  609. last = item;
  610. continue;
  611. }
  612. hide:
  613. if (item && item->menu == child) {
  614. last = (ConfigItem *)topLevelItem(0);
  615. if (last == item)
  616. last = 0;
  617. else while (last->nextSibling() != item)
  618. last = last->nextSibling();
  619. delete item;
  620. }
  621. }
  622. }
  623. void ConfigList::keyPressEvent(QKeyEvent* ev)
  624. {
  625. QTreeWidgetItem* i = currentItem();
  626. ConfigItem* item;
  627. struct menu *menu;
  628. enum prop_type type;
  629. if (ev->key() == Qt::Key_Escape && mode == singleMode) {
  630. emit parentSelected();
  631. ev->accept();
  632. return;
  633. }
  634. if (!i) {
  635. Parent::keyPressEvent(ev);
  636. return;
  637. }
  638. item = (ConfigItem*)i;
  639. switch (ev->key()) {
  640. case Qt::Key_Return:
  641. case Qt::Key_Enter:
  642. if (item->goParent) {
  643. emit parentSelected();
  644. break;
  645. }
  646. menu = item->menu;
  647. if (!menu)
  648. break;
  649. type = menu->prompt ? menu->prompt->type : P_UNKNOWN;
  650. if (type == P_MENU && rootEntry != menu &&
  651. mode != fullMode && mode != menuMode) {
  652. if (mode == menuMode)
  653. emit menuSelected(menu);
  654. else
  655. emit itemSelected(menu);
  656. break;
  657. }
  658. case Qt::Key_Space:
  659. changeValue(item);
  660. break;
  661. case Qt::Key_N:
  662. setValue(item, no);
  663. break;
  664. case Qt::Key_M:
  665. setValue(item, mod);
  666. break;
  667. case Qt::Key_Y:
  668. setValue(item, yes);
  669. break;
  670. default:
  671. Parent::keyPressEvent(ev);
  672. return;
  673. }
  674. ev->accept();
  675. }
  676. void ConfigList::mouseReleaseEvent(QMouseEvent* e)
  677. {
  678. QPoint p = e->pos();
  679. ConfigItem* item = (ConfigItem*)itemAt(p);
  680. struct menu *menu;
  681. enum prop_type ptype;
  682. QIcon icon;
  683. int idx, x;
  684. if (!item)
  685. goto skip;
  686. menu = item->menu;
  687. x = header()->offset() + p.x();
  688. idx = header()->logicalIndexAt(x);
  689. switch (idx) {
  690. case promptColIdx:
  691. icon = item->icon(promptColIdx);
  692. if (!icon.isNull()) {
  693. int off = header()->sectionPosition(0) + visualRect(indexAt(p)).x() + 4; // 4 is Hardcoded image offset. There might be a way to do it properly.
  694. if (x >= off && x < off + icon.availableSizes().first().width()) {
  695. if (item->goParent) {
  696. emit parentSelected();
  697. break;
  698. } else if (!menu)
  699. break;
  700. ptype = menu->prompt ? menu->prompt->type : P_UNKNOWN;
  701. if (ptype == P_MENU && rootEntry != menu &&
  702. mode != fullMode && mode != menuMode &&
  703. mode != listMode)
  704. emit menuSelected(menu);
  705. else
  706. changeValue(item);
  707. }
  708. }
  709. break;
  710. case dataColIdx:
  711. changeValue(item);
  712. break;
  713. }
  714. skip:
  715. //printf("contentsMouseReleaseEvent: %d,%d\n", p.x(), p.y());
  716. Parent::mouseReleaseEvent(e);
  717. }
  718. void ConfigList::mouseDoubleClickEvent(QMouseEvent* e)
  719. {
  720. QPoint p = e->pos();
  721. ConfigItem* item = (ConfigItem*)itemAt(p);
  722. struct menu *menu;
  723. enum prop_type ptype;
  724. if (!item)
  725. goto skip;
  726. if (item->goParent) {
  727. emit parentSelected();
  728. goto skip;
  729. }
  730. menu = item->menu;
  731. if (!menu)
  732. goto skip;
  733. ptype = menu->prompt ? menu->prompt->type : P_UNKNOWN;
  734. if (ptype == P_MENU && mode != listMode) {
  735. if (mode == singleMode)
  736. emit itemSelected(menu);
  737. else if (mode == symbolMode)
  738. emit menuSelected(menu);
  739. } else if (menu->sym)
  740. changeValue(item);
  741. skip:
  742. //printf("contentsMouseDoubleClickEvent: %d,%d\n", p.x(), p.y());
  743. Parent::mouseDoubleClickEvent(e);
  744. }
  745. void ConfigList::focusInEvent(QFocusEvent *e)
  746. {
  747. struct menu *menu = NULL;
  748. Parent::focusInEvent(e);
  749. ConfigItem* item = (ConfigItem *)currentItem();
  750. if (item) {
  751. setSelected(item, true);
  752. menu = item->menu;
  753. }
  754. emit gotFocus(menu);
  755. }
  756. void ConfigList::contextMenuEvent(QContextMenuEvent *e)
  757. {
  758. if (!headerPopup) {
  759. QAction *action;
  760. headerPopup = new QMenu(this);
  761. action = new QAction("Show Name", this);
  762. action->setCheckable(true);
  763. connect(action, &QAction::toggled,
  764. this, &ConfigList::setShowName);
  765. connect(this, &ConfigList::showNameChanged,
  766. action, &QAction::setChecked);
  767. action->setChecked(showName);
  768. headerPopup->addAction(action);
  769. }
  770. headerPopup->exec(e->globalPos());
  771. e->accept();
  772. }
  773. void ConfigList::setShowName(bool on)
  774. {
  775. if (showName == on)
  776. return;
  777. showName = on;
  778. reinit();
  779. emit showNameChanged(on);
  780. }
  781. QList<ConfigList *> ConfigList::allLists;
  782. QAction *ConfigList::showNormalAction;
  783. QAction *ConfigList::showAllAction;
  784. QAction *ConfigList::showPromptAction;
  785. void ConfigList::setAllOpen(bool open)
  786. {
  787. QTreeWidgetItemIterator it(this);
  788. while (*it) {
  789. (*it)->setExpanded(open);
  790. ++it;
  791. }
  792. }
  793. ConfigInfoView::ConfigInfoView(QWidget* parent, const char *name)
  794. : Parent(parent), sym(0), _menu(0)
  795. {
  796. setObjectName(name);
  797. setOpenLinks(false);
  798. if (!objectName().isEmpty()) {
  799. configSettings->beginGroup(objectName());
  800. setShowDebug(configSettings->value("/showDebug", false).toBool());
  801. configSettings->endGroup();
  802. connect(configApp, &QApplication::aboutToQuit,
  803. this, &ConfigInfoView::saveSettings);
  804. }
  805. contextMenu = createStandardContextMenu();
  806. QAction *action = new QAction("Show Debug Info", contextMenu);
  807. action->setCheckable(true);
  808. connect(action, &QAction::toggled,
  809. this, &ConfigInfoView::setShowDebug);
  810. connect(this, &ConfigInfoView::showDebugChanged,
  811. action, &QAction::setChecked);
  812. action->setChecked(showDebug());
  813. contextMenu->addSeparator();
  814. contextMenu->addAction(action);
  815. }
  816. void ConfigInfoView::saveSettings(void)
  817. {
  818. if (!objectName().isEmpty()) {
  819. configSettings->beginGroup(objectName());
  820. configSettings->setValue("/showDebug", showDebug());
  821. configSettings->endGroup();
  822. }
  823. }
  824. void ConfigInfoView::setShowDebug(bool b)
  825. {
  826. if (_showDebug != b) {
  827. _showDebug = b;
  828. if (_menu)
  829. menuInfo();
  830. else if (sym)
  831. symbolInfo();
  832. emit showDebugChanged(b);
  833. }
  834. }
  835. void ConfigInfoView::setInfo(struct menu *m)
  836. {
  837. if (_menu == m)
  838. return;
  839. _menu = m;
  840. sym = NULL;
  841. if (!_menu)
  842. clear();
  843. else
  844. menuInfo();
  845. }
  846. void ConfigInfoView::symbolInfo(void)
  847. {
  848. QString str;
  849. str += "<big>Symbol: <b>";
  850. str += print_filter(sym->name);
  851. str += "</b></big><br><br>value: ";
  852. str += print_filter(sym_get_string_value(sym));
  853. str += "<br>visibility: ";
  854. str += sym->visible == yes ? "y" : sym->visible == mod ? "m" : "n";
  855. str += "<br>";
  856. str += debug_info(sym);
  857. setText(str);
  858. }
  859. void ConfigInfoView::menuInfo(void)
  860. {
  861. struct symbol* sym;
  862. QString info;
  863. QTextStream stream(&info);
  864. sym = _menu->sym;
  865. if (sym) {
  866. if (_menu->prompt) {
  867. stream << "<big><b>";
  868. stream << print_filter(_menu->prompt->text);
  869. stream << "</b></big>";
  870. if (sym->name) {
  871. stream << " (";
  872. if (showDebug())
  873. stream << "<a href=\"" << sym->name << "\">";
  874. stream << print_filter(sym->name);
  875. if (showDebug())
  876. stream << "</a>";
  877. stream << ")";
  878. }
  879. } else if (sym->name) {
  880. stream << "<big><b>";
  881. if (showDebug())
  882. stream << "<a href=\"" << sym->name << "\">";
  883. stream << print_filter(sym->name);
  884. if (showDebug())
  885. stream << "</a>";
  886. stream << "</b></big>";
  887. }
  888. stream << "<br><br>";
  889. if (showDebug())
  890. stream << debug_info(sym);
  891. struct gstr help_gstr = str_new();
  892. menu_get_ext_help(_menu, &help_gstr);
  893. stream << print_filter(str_get(&help_gstr));
  894. str_free(&help_gstr);
  895. } else if (_menu->prompt) {
  896. stream << "<big><b>";
  897. stream << print_filter(_menu->prompt->text);
  898. stream << "</b></big><br><br>";
  899. if (showDebug()) {
  900. if (_menu->prompt->visible.expr) {
  901. stream << "&nbsp;&nbsp;dep: ";
  902. expr_print(_menu->prompt->visible.expr,
  903. expr_print_help, &stream, E_NONE);
  904. stream << "<br><br>";
  905. }
  906. stream << "defined at " << _menu->filename << ":"
  907. << _menu->lineno << "<br><br>";
  908. }
  909. }
  910. setText(info);
  911. }
  912. QString ConfigInfoView::debug_info(struct symbol *sym)
  913. {
  914. QString debug;
  915. QTextStream stream(&debug);
  916. stream << "type: ";
  917. stream << print_filter(sym_type_name(sym->type));
  918. if (sym_is_choice(sym))
  919. stream << " (choice)";
  920. debug += "<br>";
  921. if (sym->rev_dep.expr) {
  922. stream << "reverse dep: ";
  923. expr_print(sym->rev_dep.expr, expr_print_help, &stream, E_NONE);
  924. stream << "<br>";
  925. }
  926. for (struct property *prop = sym->prop; prop; prop = prop->next) {
  927. switch (prop->type) {
  928. case P_PROMPT:
  929. case P_MENU:
  930. stream << "prompt: ";
  931. stream << print_filter(prop->text);
  932. stream << "<br>";
  933. break;
  934. case P_DEFAULT:
  935. case P_SELECT:
  936. case P_RANGE:
  937. case P_COMMENT:
  938. case P_IMPLY:
  939. stream << prop_get_type_name(prop->type);
  940. stream << ": ";
  941. expr_print(prop->expr, expr_print_help,
  942. &stream, E_NONE);
  943. stream << "<br>";
  944. break;
  945. default:
  946. stream << "unknown property: ";
  947. stream << prop_get_type_name(prop->type);
  948. stream << "<br>";
  949. }
  950. if (prop->visible.expr) {
  951. stream << "&nbsp;&nbsp;&nbsp;&nbsp;dep: ";
  952. expr_print(prop->visible.expr, expr_print_help,
  953. &stream, E_NONE);
  954. stream << "<br>";
  955. }
  956. }
  957. stream << "<br>";
  958. return debug;
  959. }
  960. QString ConfigInfoView::print_filter(const QString &str)
  961. {
  962. QRegularExpression re("[<>&\"\\n]");
  963. QString res = str;
  964. QHash<QChar, QString> patterns;
  965. patterns['<'] = "&lt;";
  966. patterns['>'] = "&gt;";
  967. patterns['&'] = "&amp;";
  968. patterns['"'] = "&quot;";
  969. patterns['\n'] = "<br>";
  970. for (int i = 0; (i = res.indexOf(re, i)) >= 0;) {
  971. const QString n = patterns.value(res[i], QString());
  972. if (!n.isEmpty()) {
  973. res.replace(i, 1, n);
  974. i += n.length();
  975. }
  976. }
  977. return res;
  978. }
  979. void ConfigInfoView::expr_print_help(void *data, struct symbol *sym, const char *str)
  980. {
  981. QTextStream *stream = reinterpret_cast<QTextStream *>(data);
  982. if (sym && sym->name && !(sym->flags & SYMBOL_CONST)) {
  983. *stream << "<a href=\"" << sym->name << "\">";
  984. *stream << print_filter(str);
  985. *stream << "</a>";
  986. } else {
  987. *stream << print_filter(str);
  988. }
  989. }
  990. void ConfigInfoView::clicked(const QUrl &url)
  991. {
  992. struct menu *m;
  993. sym = sym_find(url.toEncoded().constData());
  994. m = sym_get_prompt_menu(sym);
  995. if (!m) {
  996. /* Symbol is not visible as a menu */
  997. symbolInfo();
  998. emit showDebugChanged(true);
  999. } else {
  1000. emit menuSelected(m);
  1001. }
  1002. }
  1003. void ConfigInfoView::contextMenuEvent(QContextMenuEvent *event)
  1004. {
  1005. contextMenu->popup(event->globalPos());
  1006. event->accept();
  1007. }
  1008. ConfigSearchWindow::ConfigSearchWindow(ConfigMainWindow *parent)
  1009. : Parent(parent), result(NULL)
  1010. {
  1011. setObjectName("search");
  1012. setWindowTitle("Search Config");
  1013. QVBoxLayout* layout1 = new QVBoxLayout(this);
  1014. layout1->setContentsMargins(11, 11, 11, 11);
  1015. layout1->setSpacing(6);
  1016. QHBoxLayout* layout2 = new QHBoxLayout();
  1017. layout2->setContentsMargins(0, 0, 0, 0);
  1018. layout2->setSpacing(6);
  1019. layout2->addWidget(new QLabel("Find:", this));
  1020. editField = new QLineEdit(this);
  1021. connect(editField, &QLineEdit::returnPressed,
  1022. this, &ConfigSearchWindow::search);
  1023. layout2->addWidget(editField);
  1024. searchButton = new QPushButton("Search", this);
  1025. searchButton->setAutoDefault(false);
  1026. connect(searchButton, &QPushButton::clicked,
  1027. this, &ConfigSearchWindow::search);
  1028. layout2->addWidget(searchButton);
  1029. layout1->addLayout(layout2);
  1030. split = new QSplitter(Qt::Vertical, this);
  1031. list = new ConfigList(split, "search");
  1032. list->mode = listMode;
  1033. info = new ConfigInfoView(split, "search");
  1034. connect(list, &ConfigList::menuChanged,
  1035. info, &ConfigInfoView::setInfo);
  1036. connect(list, &ConfigList::menuChanged,
  1037. parent, &ConfigMainWindow::setMenuLink);
  1038. layout1->addWidget(split);
  1039. QVariant x, y;
  1040. int width, height;
  1041. bool ok;
  1042. configSettings->beginGroup("search");
  1043. width = configSettings->value("/window width", parent->width() / 2).toInt();
  1044. height = configSettings->value("/window height", parent->height() / 2).toInt();
  1045. resize(width, height);
  1046. x = configSettings->value("/window x");
  1047. y = configSettings->value("/window y");
  1048. if (x.isValid() && y.isValid())
  1049. move(x.toInt(), y.toInt());
  1050. QList<int> sizes = configSettings->readSizes("/split", &ok);
  1051. if (ok)
  1052. split->setSizes(sizes);
  1053. configSettings->endGroup();
  1054. connect(configApp, &QApplication::aboutToQuit,
  1055. this, &ConfigSearchWindow::saveSettings);
  1056. }
  1057. void ConfigSearchWindow::saveSettings(void)
  1058. {
  1059. if (!objectName().isEmpty()) {
  1060. configSettings->beginGroup(objectName());
  1061. configSettings->setValue("/window x", pos().x());
  1062. configSettings->setValue("/window y", pos().y());
  1063. configSettings->setValue("/window width", size().width());
  1064. configSettings->setValue("/window height", size().height());
  1065. configSettings->writeSizes("/split", split->sizes());
  1066. configSettings->endGroup();
  1067. }
  1068. }
  1069. void ConfigSearchWindow::search(void)
  1070. {
  1071. struct symbol **p;
  1072. struct property *prop;
  1073. ConfigItem *lastItem = NULL;
  1074. free(result);
  1075. list->clear();
  1076. info->clear();
  1077. result = sym_re_search(editField->text().toLatin1());
  1078. if (!result)
  1079. return;
  1080. for (p = result; *p; p++) {
  1081. for_all_prompts((*p), prop)
  1082. lastItem = new ConfigItem(list, lastItem, prop->menu);
  1083. }
  1084. }
  1085. /*
  1086. * Construct the complete config widget
  1087. */
  1088. ConfigMainWindow::ConfigMainWindow(void)
  1089. : searchWindow(0)
  1090. {
  1091. bool ok = true;
  1092. QVariant x, y;
  1093. int width, height;
  1094. char title[256];
  1095. snprintf(title, sizeof(title), "%s%s",
  1096. rootmenu.prompt->text,
  1097. ""
  1098. );
  1099. setWindowTitle(title);
  1100. QRect g = configApp->primaryScreen()->geometry();
  1101. width = configSettings->value("/window width", g.width() - 64).toInt();
  1102. height = configSettings->value("/window height", g.height() - 64).toInt();
  1103. resize(width, height);
  1104. x = configSettings->value("/window x");
  1105. y = configSettings->value("/window y");
  1106. if ((x.isValid())&&(y.isValid()))
  1107. move(x.toInt(), y.toInt());
  1108. // set up icons
  1109. QString iconsDir = QString(getenv(SRCTREE) ? getenv(SRCTREE) : QDir::currentPath()) + "/scripts/kconfig/icons/";
  1110. ConfigItem::symbolYesIcon = QIcon(QPixmap(iconsDir + "symbol_yes.xpm"));
  1111. ConfigItem::symbolModIcon = QIcon(QPixmap(iconsDir + "symbol_mod.xpm"));
  1112. ConfigItem::symbolNoIcon = QIcon(QPixmap(iconsDir + "symbol_no.xpm"));
  1113. ConfigItem::choiceYesIcon = QIcon(QPixmap(iconsDir + "choice_yes.xpm"));
  1114. ConfigItem::choiceNoIcon = QIcon(QPixmap(iconsDir + "choice_no.xpm"));
  1115. ConfigItem::menuIcon = QIcon(QPixmap(iconsDir + "menu.xpm"));
  1116. ConfigItem::menubackIcon = QIcon(QPixmap(iconsDir + "menuback.xpm"));
  1117. QWidget *widget = new QWidget(this);
  1118. setCentralWidget(widget);
  1119. QVBoxLayout *layout = new QVBoxLayout(widget);
  1120. split2 = new QSplitter(Qt::Vertical, widget);
  1121. layout->addWidget(split2);
  1122. split2->setChildrenCollapsible(false);
  1123. split1 = new QSplitter(Qt::Horizontal, split2);
  1124. split1->setChildrenCollapsible(false);
  1125. configList = new ConfigList(split1, "config");
  1126. menuList = new ConfigList(split1, "menu");
  1127. helpText = new ConfigInfoView(split2, "help");
  1128. setTabOrder(configList, helpText);
  1129. configList->setFocus();
  1130. backAction = new QAction(QPixmap(iconsDir + "back.xpm"), "Back", this);
  1131. backAction->setShortcut(QKeySequence::Back);
  1132. connect(backAction, &QAction::triggered,
  1133. this, &ConfigMainWindow::goBack);
  1134. QAction *quitAction = new QAction("&Quit", this);
  1135. quitAction->setShortcut(QKeySequence::Quit);
  1136. connect(quitAction, &QAction::triggered,
  1137. this, &ConfigMainWindow::close);
  1138. QAction *loadAction = new QAction(QPixmap(iconsDir + "load.xpm"), "&Open", this);
  1139. loadAction->setShortcut(QKeySequence::Open);
  1140. connect(loadAction, &QAction::triggered,
  1141. this, &ConfigMainWindow::loadConfig);
  1142. saveAction = new QAction(QPixmap(iconsDir + "save.xpm"), "&Save", this);
  1143. saveAction->setShortcut(QKeySequence::Save);
  1144. connect(saveAction, &QAction::triggered,
  1145. this, &ConfigMainWindow::saveConfig);
  1146. conf_set_changed_callback(conf_changed);
  1147. configname = conf_get_configname();
  1148. QAction *saveAsAction = new QAction("Save &As...", this);
  1149. saveAsAction->setShortcut(QKeySequence::SaveAs);
  1150. connect(saveAsAction, &QAction::triggered,
  1151. this, &ConfigMainWindow::saveConfigAs);
  1152. QAction *searchAction = new QAction("&Find", this);
  1153. searchAction->setShortcut(QKeySequence::Find);
  1154. connect(searchAction, &QAction::triggered,
  1155. this, &ConfigMainWindow::searchConfig);
  1156. singleViewAction = new QAction(QPixmap(iconsDir + "single_view.xpm"), "Single View", this);
  1157. singleViewAction->setCheckable(true);
  1158. connect(singleViewAction, &QAction::triggered,
  1159. this, &ConfigMainWindow::showSingleView);
  1160. splitViewAction = new QAction(QPixmap(iconsDir + "split_view.xpm"), "Split View", this);
  1161. splitViewAction->setCheckable(true);
  1162. connect(splitViewAction, &QAction::triggered,
  1163. this, &ConfigMainWindow::showSplitView);
  1164. fullViewAction = new QAction(QPixmap(iconsDir + "tree_view.xpm"), "Full View", this);
  1165. fullViewAction->setCheckable(true);
  1166. connect(fullViewAction, &QAction::triggered,
  1167. this, &ConfigMainWindow::showFullView);
  1168. QAction *showNameAction = new QAction("Show Name", this);
  1169. showNameAction->setCheckable(true);
  1170. connect(showNameAction, &QAction::toggled,
  1171. configList, &ConfigList::setShowName);
  1172. showNameAction->setChecked(configList->showName);
  1173. QActionGroup *optGroup = new QActionGroup(this);
  1174. optGroup->setExclusive(true);
  1175. connect(optGroup, &QActionGroup::triggered,
  1176. configList, &ConfigList::setOptionMode);
  1177. connect(optGroup, &QActionGroup::triggered,
  1178. menuList, &ConfigList::setOptionMode);
  1179. ConfigList::showNormalAction = new QAction("Show Normal Options", optGroup);
  1180. ConfigList::showNormalAction->setCheckable(true);
  1181. ConfigList::showAllAction = new QAction("Show All Options", optGroup);
  1182. ConfigList::showAllAction->setCheckable(true);
  1183. ConfigList::showPromptAction = new QAction("Show Prompt Options", optGroup);
  1184. ConfigList::showPromptAction->setCheckable(true);
  1185. switch (configList->optMode) {
  1186. case allOpt:
  1187. ConfigList::showAllAction->setChecked(true);
  1188. break;
  1189. case promptOpt:
  1190. ConfigList::showPromptAction->setChecked(true);
  1191. break;
  1192. case normalOpt:
  1193. default:
  1194. ConfigList::showNormalAction->setChecked(true);
  1195. break;
  1196. }
  1197. QAction *showDebugAction = new QAction("Show Debug Info", this);
  1198. showDebugAction->setCheckable(true);
  1199. connect(showDebugAction, &QAction::toggled,
  1200. helpText, &ConfigInfoView::setShowDebug);
  1201. showDebugAction->setChecked(helpText->showDebug());
  1202. QAction *showIntroAction = new QAction("Introduction", this);
  1203. connect(showIntroAction, &QAction::triggered,
  1204. this, &ConfigMainWindow::showIntro);
  1205. QAction *showAboutAction = new QAction("About", this);
  1206. connect(showAboutAction, &QAction::triggered,
  1207. this, &ConfigMainWindow::showAbout);
  1208. // init tool bar
  1209. QToolBar *toolBar = addToolBar("Tools");
  1210. toolBar->addAction(backAction);
  1211. toolBar->addSeparator();
  1212. toolBar->addAction(loadAction);
  1213. toolBar->addAction(saveAction);
  1214. toolBar->addSeparator();
  1215. toolBar->addAction(singleViewAction);
  1216. toolBar->addAction(splitViewAction);
  1217. toolBar->addAction(fullViewAction);
  1218. // create file menu
  1219. QMenu *menu = menuBar()->addMenu("&File");
  1220. menu->addAction(loadAction);
  1221. menu->addAction(saveAction);
  1222. menu->addAction(saveAsAction);
  1223. menu->addSeparator();
  1224. menu->addAction(quitAction);
  1225. // create edit menu
  1226. menu = menuBar()->addMenu("&Edit");
  1227. menu->addAction(searchAction);
  1228. // create options menu
  1229. menu = menuBar()->addMenu("&Option");
  1230. menu->addAction(showNameAction);
  1231. menu->addSeparator();
  1232. menu->addActions(optGroup->actions());
  1233. menu->addSeparator();
  1234. menu->addAction(showDebugAction);
  1235. // create help menu
  1236. menu = menuBar()->addMenu("&Help");
  1237. menu->addAction(showIntroAction);
  1238. menu->addAction(showAboutAction);
  1239. connect(helpText, &ConfigInfoView::anchorClicked,
  1240. helpText, &ConfigInfoView::clicked);
  1241. connect(configList, &ConfigList::menuChanged,
  1242. helpText, &ConfigInfoView::setInfo);
  1243. connect(configList, &ConfigList::menuSelected,
  1244. this, &ConfigMainWindow::changeMenu);
  1245. connect(configList, &ConfigList::itemSelected,
  1246. this, &ConfigMainWindow::changeItens);
  1247. connect(configList, &ConfigList::parentSelected,
  1248. this, &ConfigMainWindow::goBack);
  1249. connect(menuList, &ConfigList::menuChanged,
  1250. helpText, &ConfigInfoView::setInfo);
  1251. connect(menuList, &ConfigList::menuSelected,
  1252. this, &ConfigMainWindow::changeMenu);
  1253. connect(configList, &ConfigList::gotFocus,
  1254. helpText, &ConfigInfoView::setInfo);
  1255. connect(menuList, &ConfigList::gotFocus,
  1256. helpText, &ConfigInfoView::setInfo);
  1257. connect(menuList, &ConfigList::gotFocus,
  1258. this, &ConfigMainWindow::listFocusChanged);
  1259. connect(helpText, &ConfigInfoView::menuSelected,
  1260. this, &ConfigMainWindow::setMenuLink);
  1261. connect(configApp, &QApplication::aboutToQuit,
  1262. this, &ConfigMainWindow::saveSettings);
  1263. conf_read(NULL);
  1264. QString listMode = configSettings->value("/listMode", "symbol").toString();
  1265. if (listMode == "single")
  1266. showSingleView();
  1267. else if (listMode == "full")
  1268. showFullView();
  1269. else /*if (listMode == "split")*/
  1270. showSplitView();
  1271. // UI setup done, restore splitter positions
  1272. QList<int> sizes = configSettings->readSizes("/split1", &ok);
  1273. if (ok)
  1274. split1->setSizes(sizes);
  1275. sizes = configSettings->readSizes("/split2", &ok);
  1276. if (ok)
  1277. split2->setSizes(sizes);
  1278. }
  1279. void ConfigMainWindow::loadConfig(void)
  1280. {
  1281. QString str;
  1282. str = QFileDialog::getOpenFileName(this, QString(), configname);
  1283. if (str.isEmpty())
  1284. return;
  1285. if (conf_read(str.toLocal8Bit().constData()))
  1286. QMessageBox::information(this, "qconf", "Unable to load configuration!");
  1287. configname = str;
  1288. ConfigList::updateListAllForAll();
  1289. }
  1290. bool ConfigMainWindow::saveConfig(void)
  1291. {
  1292. if (conf_write(configname.toLocal8Bit().constData())) {
  1293. QMessageBox::information(this, "qconf", "Unable to save configuration!");
  1294. return false;
  1295. }
  1296. conf_write_autoconf(0);
  1297. return true;
  1298. }
  1299. void ConfigMainWindow::saveConfigAs(void)
  1300. {
  1301. QString str;
  1302. str = QFileDialog::getSaveFileName(this, QString(), configname);
  1303. if (str.isEmpty())
  1304. return;
  1305. if (conf_write(str.toLocal8Bit().constData())) {
  1306. QMessageBox::information(this, "qconf", "Unable to save configuration!");
  1307. }
  1308. conf_write_autoconf(0);
  1309. configname = str;
  1310. }
  1311. void ConfigMainWindow::searchConfig(void)
  1312. {
  1313. if (!searchWindow)
  1314. searchWindow = new ConfigSearchWindow(this);
  1315. searchWindow->show();
  1316. }
  1317. void ConfigMainWindow::changeItens(struct menu *menu)
  1318. {
  1319. configList->setRootMenu(menu);
  1320. }
  1321. void ConfigMainWindow::changeMenu(struct menu *menu)
  1322. {
  1323. menuList->setRootMenu(menu);
  1324. }
  1325. void ConfigMainWindow::setMenuLink(struct menu *menu)
  1326. {
  1327. struct menu *parent;
  1328. ConfigList* list = NULL;
  1329. ConfigItem* item;
  1330. if (configList->menuSkip(menu))
  1331. return;
  1332. switch (configList->mode) {
  1333. case singleMode:
  1334. list = configList;
  1335. parent = menu_get_menu_or_parent_menu(menu);
  1336. if (!parent)
  1337. return;
  1338. list->setRootMenu(parent);
  1339. break;
  1340. case menuMode:
  1341. if (menu->flags & MENU_ROOT) {
  1342. menuList->setRootMenu(menu);
  1343. configList->clearSelection();
  1344. list = configList;
  1345. } else {
  1346. parent = menu_get_menu_or_parent_menu(menu->parent);
  1347. if (!parent)
  1348. return;
  1349. /* Select the config view */
  1350. item = configList->findConfigItem(parent);
  1351. if (item) {
  1352. configList->setSelected(item, true);
  1353. configList->scrollToItem(item);
  1354. }
  1355. menuList->setRootMenu(parent);
  1356. menuList->clearSelection();
  1357. list = menuList;
  1358. }
  1359. break;
  1360. case fullMode:
  1361. list = configList;
  1362. break;
  1363. default:
  1364. break;
  1365. }
  1366. if (list) {
  1367. item = list->findConfigItem(menu);
  1368. if (item) {
  1369. list->setSelected(item, true);
  1370. list->scrollToItem(item);
  1371. list->setFocus();
  1372. helpText->setInfo(menu);
  1373. }
  1374. }
  1375. }
  1376. void ConfigMainWindow::listFocusChanged(void)
  1377. {
  1378. if (menuList->mode == menuMode)
  1379. configList->clearSelection();
  1380. }
  1381. void ConfigMainWindow::goBack(void)
  1382. {
  1383. configList->setParentMenu();
  1384. }
  1385. void ConfigMainWindow::showSingleView(void)
  1386. {
  1387. singleViewAction->setEnabled(false);
  1388. singleViewAction->setChecked(true);
  1389. splitViewAction->setEnabled(true);
  1390. splitViewAction->setChecked(false);
  1391. fullViewAction->setEnabled(true);
  1392. fullViewAction->setChecked(false);
  1393. backAction->setEnabled(true);
  1394. menuList->hide();
  1395. menuList->setRootMenu(0);
  1396. configList->mode = singleMode;
  1397. if (configList->rootEntry == &rootmenu)
  1398. configList->updateListAll();
  1399. else
  1400. configList->setRootMenu(&rootmenu);
  1401. configList->setFocus();
  1402. }
  1403. void ConfigMainWindow::showSplitView(void)
  1404. {
  1405. singleViewAction->setEnabled(true);
  1406. singleViewAction->setChecked(false);
  1407. splitViewAction->setEnabled(false);
  1408. splitViewAction->setChecked(true);
  1409. fullViewAction->setEnabled(true);
  1410. fullViewAction->setChecked(false);
  1411. backAction->setEnabled(false);
  1412. configList->mode = menuMode;
  1413. if (configList->rootEntry == &rootmenu)
  1414. configList->updateListAll();
  1415. else
  1416. configList->setRootMenu(&rootmenu);
  1417. configList->setAllOpen(true);
  1418. configApp->processEvents();
  1419. menuList->mode = symbolMode;
  1420. menuList->setRootMenu(&rootmenu);
  1421. menuList->setAllOpen(true);
  1422. menuList->show();
  1423. menuList->setFocus();
  1424. }
  1425. void ConfigMainWindow::showFullView(void)
  1426. {
  1427. singleViewAction->setEnabled(true);
  1428. singleViewAction->setChecked(false);
  1429. splitViewAction->setEnabled(true);
  1430. splitViewAction->setChecked(false);
  1431. fullViewAction->setEnabled(false);
  1432. fullViewAction->setChecked(true);
  1433. backAction->setEnabled(false);
  1434. menuList->hide();
  1435. menuList->setRootMenu(0);
  1436. configList->mode = fullMode;
  1437. if (configList->rootEntry == &rootmenu)
  1438. configList->updateListAll();
  1439. else
  1440. configList->setRootMenu(&rootmenu);
  1441. configList->setFocus();
  1442. }
  1443. /*
  1444. * ask for saving configuration before quitting
  1445. */
  1446. void ConfigMainWindow::closeEvent(QCloseEvent* e)
  1447. {
  1448. if (!conf_get_changed()) {
  1449. e->accept();
  1450. return;
  1451. }
  1452. QMessageBox mb(QMessageBox::Icon::Warning, "qconf",
  1453. "Save configuration?");
  1454. QPushButton *yb = mb.addButton(QMessageBox::Yes);
  1455. QPushButton *db = mb.addButton(QMessageBox::No);
  1456. QPushButton *cb = mb.addButton(QMessageBox::Cancel);
  1457. yb->setText("&Save Changes");
  1458. db->setText("&Discard Changes");
  1459. cb->setText("Cancel Exit");
  1460. mb.setDefaultButton(yb);
  1461. mb.setEscapeButton(cb);
  1462. switch (mb.exec()) {
  1463. case QMessageBox::Yes:
  1464. if (saveConfig())
  1465. e->accept();
  1466. else
  1467. e->ignore();
  1468. break;
  1469. case QMessageBox::No:
  1470. e->accept();
  1471. break;
  1472. case QMessageBox::Cancel:
  1473. e->ignore();
  1474. break;
  1475. }
  1476. }
  1477. void ConfigMainWindow::showIntro(void)
  1478. {
  1479. static const QString str =
  1480. "Welcome to the qconf graphical configuration tool.\n"
  1481. "\n"
  1482. "For bool and tristate options, a blank box indicates the "
  1483. "feature is disabled, a check indicates it is enabled, and a "
  1484. "dot indicates that it is to be compiled as a module. Clicking "
  1485. "on the box will cycle through the three states. For int, hex, "
  1486. "and string options, double-clicking or pressing F2 on the "
  1487. "Value cell will allow you to edit the value.\n"
  1488. "\n"
  1489. "If you do not see an option (e.g., a device driver) that you "
  1490. "believe should be present, try turning on Show All Options "
  1491. "under the Options menu. Enabling Show Debug Info will help you"
  1492. "figure out what other options must be enabled to support the "
  1493. "option you are interested in, and hyperlinks will navigate to "
  1494. "them.\n"
  1495. "\n"
  1496. "Toggling Show Debug Info under the Options menu will show the "
  1497. "dependencies, which you can then match by examining other "
  1498. "options.\n";
  1499. QMessageBox::information(this, "qconf", str);
  1500. }
  1501. void ConfigMainWindow::showAbout(void)
  1502. {
  1503. static const QString str = "qconf is Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>.\n"
  1504. "Copyright (C) 2015 Boris Barbulovski <bbarbulovski@gmail.com>.\n"
  1505. "\n"
  1506. "Bug reports and feature request can also be entered at http://bugzilla.kernel.org/\n"
  1507. "\n"
  1508. "Qt Version: ";
  1509. QMessageBox::information(this, "qconf", str + qVersion());
  1510. }
  1511. void ConfigMainWindow::saveSettings(void)
  1512. {
  1513. configSettings->setValue("/window x", pos().x());
  1514. configSettings->setValue("/window y", pos().y());
  1515. configSettings->setValue("/window width", size().width());
  1516. configSettings->setValue("/window height", size().height());
  1517. QString entry;
  1518. switch(configList->mode) {
  1519. case singleMode :
  1520. entry = "single";
  1521. break;
  1522. case symbolMode :
  1523. entry = "split";
  1524. break;
  1525. case fullMode :
  1526. entry = "full";
  1527. break;
  1528. default:
  1529. break;
  1530. }
  1531. configSettings->setValue("/listMode", entry);
  1532. configSettings->writeSizes("/split1", split1->sizes());
  1533. configSettings->writeSizes("/split2", split2->sizes());
  1534. }
  1535. void ConfigMainWindow::conf_changed(bool dirty)
  1536. {
  1537. if (saveAction)
  1538. saveAction->setEnabled(dirty);
  1539. }
  1540. void fixup_rootmenu(struct menu *menu)
  1541. {
  1542. struct menu *child;
  1543. static int menu_cnt = 0;
  1544. menu->flags |= MENU_ROOT;
  1545. for (child = menu->list; child; child = child->next) {
  1546. if (child->prompt && child->prompt->type == P_MENU) {
  1547. menu_cnt++;
  1548. fixup_rootmenu(child);
  1549. menu_cnt--;
  1550. } else if (!menu_cnt)
  1551. fixup_rootmenu(child);
  1552. }
  1553. }
  1554. static const char *progname;
  1555. static void usage(void)
  1556. {
  1557. printf("%s [-s] <config>\n", progname);
  1558. exit(0);
  1559. }
  1560. int main(int ac, char** av)
  1561. {
  1562. ConfigMainWindow* v;
  1563. const char *name;
  1564. progname = av[0];
  1565. if (ac > 1 && av[1][0] == '-') {
  1566. switch (av[1][1]) {
  1567. case 's':
  1568. conf_set_message_callback(NULL);
  1569. break;
  1570. case 'h':
  1571. case '?':
  1572. usage();
  1573. }
  1574. name = av[2];
  1575. } else
  1576. name = av[1];
  1577. if (!name)
  1578. usage();
  1579. conf_parse(name);
  1580. fixup_rootmenu(&rootmenu);
  1581. //zconfdump(stdout);
  1582. configApp = new QApplication(ac, av);
  1583. configSettings = new ConfigSettings();
  1584. v = new ConfigMainWindow();
  1585. //zconfdump(stdout);
  1586. v->show();
  1587. configApp->exec();
  1588. delete configSettings;
  1589. delete v;
  1590. delete configApp;
  1591. return 0;
  1592. }