qtlangset.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. #include "qtlangset.h"
  2. #include "scintillaeditview.h"
  3. #include "jsondeploy.h"
  4. #include <qscilexer.h>
  5. #include <QSettings>
  6. #include <QColorDialog>
  7. #include <QMessageBox>
  8. #include <QSpinBox>
  9. #include <qDebug>
  10. #if 0
  11. //要注意与common.h的保持一致。
  12. enum LangType {
  13. L_TEXT = -1, L_PHP, L_C, L_CPP, L_CS, L_OBJC, L_JAVA, L_RC, \
  14. L_HTML, L_XML, L_MAKEFILE, L_PASCAL, L_BATCH, L_INI, L_ASCII, L_USER, \
  15. L_ASP, L_SQL, L_VB, L_JS, L_CSS, L_PERL, L_PYTHON, L_LUA, \
  16. L_TEX, L_FORTRAN, L_BASH, L_FLASH, L_NSIS, L_TCL, L_LISP, L_SCHEME, \
  17. L_ASM, L_DIFF, L_PROPS, L_PS, L_RUBY, L_SMALLTALK, L_VHDL, L_KIX, L_AU3, \
  18. L_CAML, L_ADA, L_VERILOG, L_MATLAB, L_HASKELL, L_INNO, L_SEARCHRESULT, \
  19. L_CMAKE, L_YAML, L_COBOL, L_GUI4CLI, L_D, L_POWERSHELL, L_R, L_JSP, \
  20. L_COFFEESCRIPT, L_JSON, L_JAVASCRIPT, L_FORTRAN_77, L_BAANC, L_SREC, \
  21. L_IHEX, L_TEHEX, L_SWIFT, \
  22. L_ASN1, L_AVS, L_BLITZBASIC, L_PUREBASIC, L_FREEBASIC, \
  23. L_CSOUND, L_ERLANG, L_ESCRIPT, L_FORTH, L_LATEX, \
  24. L_MMIXAL, L_NIM, L_NNCRONTAB, L_OSCRIPT, L_REBOL, \
  25. L_REGISTRY, L_RUST, L_SPICE, L_TXT2TAGS, L_VISUALPROLOG, L_TYPESCRIPT, \
  26. L_EDIFACT, L_MARKDOWN, L_OCTAVE, L_PO, L_POV, L_IDL, L_GO, L_TXT, \
  27. // Don't use L_JS, use L_JAVASCRIPT instead
  28. // The end of enumated language type, so it should be always at the end
  29. L_EXTERNAL = 100,
  30. };
  31. #endif
  32. QtLangSet::QtLangSet(QString initTag, QWidget *parent)
  33. : QMainWindow(parent), m_selectLexer(nullptr), m_selectStyleId(0), m_isStyleChange(false),m_isStyleChildChange(false), m_initShowLexerTag(initTag), m_first(true)
  34. {
  35. ui.setupUi(this);
  36. initLangList();
  37. startSignSlot();
  38. }
  39. QtLangSet::~QtLangSet()
  40. {
  41. if (nullptr != m_selectLexer)
  42. {
  43. delete m_selectLexer;
  44. m_selectLexer = nullptr;
  45. }
  46. }
  47. //本来是放在构造函数中,发现show后会导致初始信号乱。干脆在外面等show完毕后再开启信号,避免初始乱
  48. void QtLangSet::startSignSlot()
  49. {
  50. connect(ui.langListWidget, &QListWidget::currentItemChanged, this, &QtLangSet::slot_itemSelect);
  51. connect(ui.styleListWidget, &QListWidget::currentItemChanged, this, &QtLangSet::slot_styleItemSelect);
  52. connect(ui.boldCheckBox, &QCheckBox::stateChanged, this, &QtLangSet::slot_fontBoldChange);
  53. connect(ui.italicCheckBox, &QCheckBox::stateChanged, this, &QtLangSet::slot_fontItalicChange);
  54. connect(ui.underlineCheckBox, &QCheckBox::stateChanged, this, &QtLangSet::slot_fontUnderlineChange);
  55. connect(ui.fontSpinBox, SIGNAL(valueChanged(int)), this, SLOT(slot_fontSizeChange(int)));
  56. connect(ui.fontComboBox, &QFontComboBox::currentFontChanged, this, &QtLangSet::slot_fontChange);
  57. }
  58. void QtLangSet::slot_fontBoldChange(int state)
  59. {
  60. //即时设置风格
  61. if (m_selectLexer != nullptr)
  62. {
  63. if (m_curStyleData.font.bold() != ((state == Qt::Unchecked) ? false : true))
  64. {
  65. m_curStyleData.font.setBold((state == Qt::Unchecked) ? false : true);
  66. m_selectLexer->setFont(m_curStyleData.font, m_selectStyleId);
  67. m_isStyleChange = true;
  68. emit viewStyleChange(m_selectLexer->lexerId(), m_selectStyleId, m_curStyleData.color, m_curStyleData.paper, m_curStyleData.font, true);
  69. }
  70. }
  71. }
  72. void QtLangSet::slot_fontItalicChange(int state)
  73. {
  74. //即时设置风格
  75. if (m_selectLexer != nullptr)
  76. {
  77. if (m_curStyleData.font.italic() != ((state == Qt::Unchecked) ? false : true))
  78. {
  79. m_curStyleData.font.setItalic((state == Qt::Unchecked) ? false : true);
  80. m_selectLexer->setFont(m_curStyleData.font, m_selectStyleId);
  81. m_isStyleChange = true;
  82. emit viewStyleChange(m_selectLexer->lexerId(), m_selectStyleId, m_curStyleData.color, m_curStyleData.paper, m_curStyleData.font, true);
  83. }
  84. }
  85. }
  86. void QtLangSet::slot_fontUnderlineChange(int state)
  87. {
  88. //即时设置风格
  89. if (m_selectLexer != nullptr)
  90. {
  91. if (m_curStyleData.font.underline() != ((state == Qt::Unchecked) ? false : true))
  92. {
  93. m_curStyleData.font.setUnderline((state == Qt::Unchecked) ? false : true);
  94. m_selectLexer->setFont(m_curStyleData.font, m_selectStyleId);
  95. m_isStyleChange = true;
  96. emit viewStyleChange(m_selectLexer->lexerId(), m_selectStyleId, m_curStyleData.color, m_curStyleData.paper, m_curStyleData.font, true);
  97. }
  98. }
  99. }
  100. void QtLangSet::slot_fontSizeChange(int v)
  101. {
  102. //即时设置风格
  103. if (m_selectLexer != nullptr)
  104. {
  105. if (m_curStyleData.font.pointSize() != v)
  106. {
  107. m_curStyleData.font.setPointSize(v);
  108. m_selectLexer->setFont(m_curStyleData.font, m_selectStyleId);
  109. m_isStyleChange = true;
  110. emit viewStyleChange(m_selectLexer->lexerId(), m_selectStyleId, m_curStyleData.color, m_curStyleData.paper, m_curStyleData.font, true);
  111. }
  112. }
  113. }
  114. void QtLangSet::slot_fontChange(const QFont &font)
  115. {
  116. //即时设置风格
  117. if (m_selectLexer != nullptr)
  118. {
  119. if (m_curStyleData.font != font)
  120. {
  121. QFont oldf = m_curStyleData.font;
  122. m_curStyleData.font = font;
  123. m_curStyleData.font.setBold(oldf.bold());
  124. m_curStyleData.font.setItalic(oldf.italic());
  125. m_curStyleData.font.setUnderline(oldf.underline());
  126. m_selectLexer->setFont(m_curStyleData.font, m_selectStyleId);
  127. m_isStyleChange = true;
  128. emit viewStyleChange(m_selectLexer->lexerId(), m_selectStyleId, m_curStyleData.color, m_curStyleData.paper, m_curStyleData.font, true);
  129. }
  130. }
  131. }
  132. void QtLangSet::initLangList()
  133. {
  134. for (int i = 0; i <= L_TXT; ++i)
  135. {
  136. QsciLexer *pLexer = ScintillaEditView::createLexer(i);
  137. if (nullptr != pLexer)
  138. {
  139. QString langName = pLexer->lexerTag();
  140. //qDebug() << langName << pLexer->lexer();
  141. QListWidgetItem *item = new QListWidgetItem(langName);
  142. item->setData(Qt::UserRole,i);
  143. ui.langListWidget->addItem(item);
  144. delete pLexer;
  145. }
  146. }
  147. //怀疑这个是队列链接的,会延迟执行。故总是导致选择的选不中,而是第一个asp。
  148. ui.langListWidget->sortItems(Qt::AscendingOrder);
  149. }
  150. //点击当前的语言item
  151. void QtLangSet::slot_itemSelect(QListWidgetItem *item, QListWidgetItem *previous)
  152. {
  153. //第一次必须以初始化lexid显示。这样做实在是因为初始化list启动后,会自动把第一个设置为当前。
  154. //丑陋的使用替换来实现。让打开时务必呈现当前edit的语法类型
  155. if (m_first && !m_initShowLexerTag.isEmpty())
  156. {
  157. m_first = false;
  158. QList<QListWidgetItem*> destItems = ui.langListWidget->findItems(m_initShowLexerTag,Qt::MatchExactly);
  159. if (!destItems.isEmpty())
  160. {
  161. item = destItems.first();
  162. ui.langListWidget->setCurrentItem(item);
  163. }
  164. }
  165. //保存前一个已经修改的。询问用户是否保存
  166. if (m_isStyleChange && previous != nullptr && m_selectLexer !=nullptr)
  167. {
  168. if (QMessageBox::Yes == QMessageBox::question(this, tr("Save Change"), tr("%1 style configuration has been modified. Do you want to save it?").arg(previous->text())))
  169. {
  170. saveCurLangSettings();
  171. }
  172. }
  173. if (item == nullptr)
  174. {
  175. return;
  176. }
  177. if (!item->isSelected())
  178. {
  179. item->setSelected(true);
  180. }
  181. int lexId = item->data(Qt::UserRole).toInt();
  182. ui.styleListWidget->clear();
  183. if (m_selectLexer != nullptr)
  184. {
  185. delete m_selectLexer;
  186. m_selectLexer = nullptr;
  187. }
  188. QsciLexer *pLexer = ScintillaEditView::createLexer(lexId);
  189. if (nullptr != pLexer)
  190. {
  191. m_selectLexer = pLexer;
  192. for (int i = 0; i <= 255; ++i)
  193. {
  194. QString desc = pLexer->description(i);
  195. if (!desc.isEmpty())
  196. {
  197. QListWidgetItem *itemtemp = new QListWidgetItem(desc);
  198. itemtemp->setData(Qt::UserRole, i);
  199. ui.styleListWidget->addItem(itemtemp);
  200. }
  201. }
  202. }
  203. //自动显示第一条的结果
  204. QListWidgetItem *itemtemp = ui.styleListWidget->item(0);
  205. if (itemtemp != nullptr)
  206. {
  207. itemtemp->setSelected(true);
  208. slot_styleItemSelect(itemtemp, nullptr);
  209. }
  210. m_isStyleChange = false;
  211. }
  212. //获取当前ui上的字体
  213. void QtLangSet::getCurUiFont(QFont& font)
  214. {
  215. font = ui.fontComboBox->currentFont();
  216. font.setBold(ui.boldCheckBox->isChecked());
  217. font.setItalic(ui.italicCheckBox->isChecked());
  218. font.setUnderline(ui.underlineCheckBox->isChecked());
  219. font.setPointSize(ui.fontSpinBox->value());
  220. }
  221. void QtLangSet::closeEvent(QCloseEvent * e)
  222. {
  223. saveCurLangSettings();
  224. }
  225. //点击当前的风格item
  226. void QtLangSet::slot_styleItemSelect(QListWidgetItem *current, QListWidgetItem *previous)
  227. {
  228. if (current != nullptr && m_selectLexer != nullptr)
  229. {
  230. int styleId = current->data(Qt::UserRole).toInt();
  231. m_selectStyleId = styleId;
  232. QsciLexer::StyleData & sd = m_selectLexer->styleData(styleId);
  233. m_curStyleData = sd;
  234. setStyleShow(sd.font, sd.color, sd.paper);
  235. m_isStyleChange = false;
  236. }
  237. }
  238. void QtLangSet::fillForegroundColor(QColor& fcColor)
  239. {
  240. QPixmap f(32, 32);
  241. f.fill(fcColor);
  242. ui.label_fc->setPixmap(f);
  243. }
  244. void QtLangSet::fillBackgroundColor(QColor &bkColor)
  245. {
  246. QPixmap b(32, 32);
  247. b.fill(bkColor);
  248. ui.label_bc->setPixmap(b);
  249. }
  250. void QtLangSet::setStyleShow(QFont& font, QColor& fcColor, QColor &bkColor)
  251. {
  252. ui.fontComboBox->setCurrentFont(font);
  253. ui.fontSpinBox->setValue(font.pointSize());
  254. ui.boldCheckBox->setChecked(font.bold());
  255. ui.italicCheckBox->setChecked(font.italic());
  256. ui.underlineCheckBox->setChecked(font.underline());
  257. fillForegroundColor(fcColor);
  258. fillBackgroundColor(bkColor);
  259. }
  260. //读取特定语言的设置
  261. bool QtLangSet::readLangSettings(QsciLexer *lexer, QString tag)
  262. {
  263. QString cfgPath = QString("notepad/%1").arg(tag);
  264. QSettings qs(QSettings::IniFormat, QSettings::UserScope, cfgPath);
  265. return lexer->readSettings(qs);
  266. }
  267. #if 0
  268. void QtLangSet::setCurSelectLang(QString tag)
  269. {
  270. QList<QListWidgetItem*> destItem = ui.langListWidget->findItems(tag, Qt::MatchExactly);
  271. if (!destItem.isEmpty())
  272. {
  273. QListWidgetItem* itemtemp = destItem.first();
  274. itemtemp->setSelected(true);
  275. //slot_itemSelect(itemtemp, nullptr);
  276. }
  277. }
  278. #endif
  279. bool QtLangSet::saveCurLangSettings()
  280. {
  281. if (m_isStyleChange && m_selectLexer != nullptr)
  282. {
  283. QString tag = m_selectLexer->lexerTag();
  284. QString cfgPath = QString("notepad/%1").arg(tag);
  285. QSettings qs(QSettings::IniFormat, QSettings::UserScope, cfgPath);
  286. m_selectLexer->writeSettings(qs);
  287. m_isStyleChange = false;
  288. return true;
  289. }
  290. return false;
  291. }
  292. //修改前景色
  293. void QtLangSet::slot_changeFgColor()
  294. {
  295. QColor color = QColorDialog::getColor(Qt::white, this, tr("Style Foreground Color"));
  296. if (color.isValid())
  297. {
  298. //当前前景色是否变化
  299. if (m_curStyleData.color != color)
  300. {
  301. m_curStyleData.color = color;
  302. fillForegroundColor(color);
  303. m_isStyleChange = true;
  304. //即时设置风格
  305. if (m_selectLexer != nullptr)
  306. {
  307. m_selectLexer->setColor(color,m_selectStyleId);
  308. emit viewStyleChange(m_selectLexer->lexerId(), m_selectStyleId, color, m_curStyleData.paper, m_curStyleData.font,false);
  309. }
  310. }
  311. }
  312. }
  313. //修改背景色
  314. void QtLangSet::slot_changeBkColor()
  315. {
  316. QColor color = QColorDialog::getColor(Qt::white, this, tr("Style Background Color"));
  317. if (color.isValid())
  318. {
  319. //当前前景色是否变化
  320. if (m_curStyleData.paper != color)
  321. {
  322. m_curStyleData.paper = color;
  323. fillBackgroundColor(color);
  324. m_isStyleChange = true;
  325. //即时设置风格
  326. if (m_selectLexer != nullptr)
  327. {
  328. m_selectLexer->setPaper(color, m_selectStyleId);
  329. emit viewStyleChange(m_selectLexer->lexerId(), m_selectStyleId, color, m_curStyleData.paper, m_curStyleData.font, false);
  330. }
  331. }
  332. }
  333. }
  334. void QtLangSet::slot_saveClick()
  335. {
  336. saveCurLangSettings();
  337. }