1 /*
2  * Copyright (C) 2019-2021 Ashar Khan <ashar786khan@gmail.com>
3  *
4  * This file is part of CP Editor.
5  *
6  * CP Editor is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * I will not be responsible if CP Editor behaves in unexpected way and
12  * causes your ratings to go down and or lose any important contest.
13  *
14  * Believe Software is "Software" and it isn't immune to bugs.
15  *
16  */
17 
18 #include "Util/QCodeEditorUtil.hpp"
19 #include "Core/EventLogger.hpp"
20 #include "Extensions/EditorTheme.hpp"
21 #include "Settings/SettingsManager.hpp"
22 #include "generated/SettingsHelper.hpp"
23 #include <QCXXHighlighter>
24 #include <QCodeEditor>
25 #include <QJavaHighlighter>
26 #include <QPythonHighlighter>
27 
28 namespace Util
29 {
applySettingsToEditor(QCodeEditor * editor,const QString & language)30 void applySettingsToEditor(QCodeEditor *editor, const QString &language)
31 {
32     LOG_INFO("Applying settings to QCodeEditor");
33 
34     editor->setTabReplace(SettingsHelper::isReplaceTabs());
35     editor->setTabReplaceSize(SettingsHelper::getTabWidth());
36     editor->setAutoIndentation(SettingsHelper::isAutoIndent());
37 
38     editor->setFont(SettingsHelper::getEditorFont());
39 
40     const int tabStop = SettingsHelper::getTabWidth();
41     QFontMetrics metric(editor->font());
42     editor->setTabReplaceSize(tabStop);
43 
44     if (SettingsHelper::isWrapText())
45         editor->setWordWrapMode(QTextOption::WordWrap);
46     else
47         editor->setWordWrapMode(QTextOption::NoWrap);
48 
49     auto *style = Extensions::EditorTheme::query(SettingsHelper::getEditorTheme());
50     if (!style)
51         style = Extensions::EditorTheme::query("Light");
52     editor->setSyntaxStyle(style);
53 
54     editor->setExtraBottomMargin(SettingsHelper::isExtraBottomMargin());
55 
56     if (language.isEmpty())
57         return;
58 
59     if (language == "Python")
60     {
61         editor->setHighlighter(new QPythonHighlighter);
62         editor->setCompleter(nullptr);
63     }
64     else if (language == "Java")
65     {
66         editor->setHighlighter(new QJavaHighlighter);
67         editor->setCompleter(nullptr);
68     }
69     else
70     {
71         editor->setHighlighter(new QCXXHighlighter);
72         editor->setCompleter(nullptr);
73     }
74 
75     QVector<QCodeEditor::Parenthesis> parentheses;
76 
77     auto list = SettingsManager::get(language + "/Parentheses").toList();
78 
79     for (auto const &var : list)
80     {
81         auto li = var.toList();
82         if (li.length() != 5)
83         {
84             LOG_ERR(INFO_OF(li.length()));
85             continue;
86         }
87 
88         auto left = li[0].toChar();
89         auto right = li[1].toChar();
90 
91         auto getFlag = [](Qt::CheckState state, bool def) {
92             switch (state)
93             {
94             case Qt::Checked:
95                 return true;
96             case Qt::PartiallyChecked:
97                 return def;
98             case Qt::Unchecked:
99                 return false;
100             default:
101                 Q_UNREACHABLE();
102             }
103         };
104 
105         bool autoComplete = getFlag(Qt::CheckState(li[2].toInt()), SettingsHelper::isAutoCompleteParentheses());
106         bool autoRemove = getFlag(Qt::CheckState(li[3].toInt()), SettingsHelper::isAutoRemoveParentheses());
107         bool tabJumpOut = getFlag(Qt::CheckState(li[4].toInt()), SettingsHelper::isTabJumpOutParentheses());
108 
109         parentheses.push_back({left, right, autoComplete, autoRemove, tabJumpOut});
110     }
111 
112     editor->setParentheses(parentheses);
113 }
114 } // namespace Util
115