Repository navigation
Expand file tree
/
Copy patheditorwidget.cpp
More file actions
138 lines (107 loc) 路 4.36 KB
/
Copy patheditorwidget.cpp
File metadata and controls
138 lines (107 loc) 路 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "editorwidget.h"
#include "qlistwidget.h"
#include "snippetpreviewbox.h"
#include "snippetsettingspopup.h"
#include "ui_editorwidget.h"
#include <qscrollbar.h>
editorWidget::editorWidget(MainWindow* mainwindow, QWidget *parent)
: QWidget(parent)
, ui(new Ui::editorWidget)
, mainWindow(mainwindow)
{
ui->setupUi(this);
this->setAttribute(Qt::WA_DeleteOnClose,true);
ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->lineNo->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding); // Prevents expansion
ui->lineNo->setAlignment(Qt::AlignTop | Qt::AlignLeft); // Aligns text properly
ui->textEdit->setWordWrapMode(QTextOption::NoWrap); // Disable word wrapping
ui->textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); // Show scrollbar when needed
//text highlighting=====================================
// QTextEdit *textEdit = new QTextEdit;
// KeywordHighlighter *highlighter = new KeywordHighlighter(textEdit->document());
// textEdit->setPlainText("int main() { for (int i = 0; i < 10; ++i) {} }");
// textEdit->show();
//KeywordHighlighter *highlighter = new KeywordHighlighter(ui->textEdit->document());
ui->textEdit->setPlainText("int main() { for (int i = 0; i < 10; ++i) {} }");
ui->lineNo->setAlignment(Qt::AlignTop | Qt::AlignLeft);
connect(ui->textEdit->verticalScrollBar(), &QScrollBar::valueChanged, this, &editorWidget::synchronizeScroll);
//connect(ui->textEdit, &QTextEdit::textChanged, this, &editorWidget::synchronizeScroll);
ui->lineNo->setContentsMargins(0, 5, 0, 0); // Top margin of 5 pixels
on_textEdit_textChanged();
ui->editorCopyButton->setIcon(QIcon(":images/copyButton.svg"));
ui->editorCopyButton->setIconSize(QSize(18,18));
ui->editorSnippetSettingsButton->setIcon(QIcon(":/images/settingsIcon.svg"));
ui->editorSnippetSettingsButton->setIconSize(QSize(21, 21));
ui->editorSaveButton->setIcon(QIcon(":/images/saveIcon.svg"));
ui->editorSaveButton->setIconSize(QSize(19, 19));
ui->tabCloseButton->setIcon(QIcon(":/images/closeIcon.svg"));
ui->tabCloseButton->setIconSize(QSize(20,20));
int fontId = QFontDatabase::addApplicationFont(":/fonts/Menlo-Regular.ttf");
if (fontId != -1) {
QString family = QFontDatabase::applicationFontFamilies(fontId).at(0);
menlo=QFont(family);
menlo.setPointSize(16);
ui->textEdit->setFont(menlo);
ui->lineNo->setFont(menlo);
} else {
qDebug() << "Font loading failed!";
}
}
editorWidget::~editorWidget()
{
delete ui;
}
void editorWidget::on_textEdit_textChanged()
{
QStringList lineNumbers;
int lineCount = ui->textEdit->document()->blockCount();
for (int i = 1; i <= lineCount; ++i) {
lineNumbers << QString::number(i);
}
ui->lineNo->setText(lineNumbers.join("\n"));
//ui->lineNo->setFixedHeight(ui->textEdit->document()->size().height());
}
void editorWidget::synchronizeScroll(int value) {
// Scroll the label's content to match the textEdit
ui->lineNo->move(ui->lineNo->x(), -value);
}
QTextDocument* editorWidget::syntaxHighlightTarget(){
return ui->textEdit->document();
}
void editorWidget::assign(snippetBaseClass* snipObj, bool isOld){
thisSnippet=snipObj;
thisSnippet->setEditor(ui->textEdit->document());
if(isOld) {
ui->textEdit->setPlainText(QString::fromStdString(thisSnippet->getSnippet()));
} else {
ui->textEdit->clear();
}
}
void editorWidget::tellIdx(int i){
idx=i;
}
void editorWidget::on_editorSnippetSettingsButton_clicked()
{
//mainWindow->warnUser("Jessan was supposed to make this!! but now i did");
snippetSettingsPopup* pop=new snippetSettingsPopup(mainWindow);
pop->assign(thisSnippet);
pop->show();
}
void editorWidget::on_editorCopyButton_clicked()
{
QString str=ui->textEdit->toPlainText();
thisSnippet->saveSnippetToFile(str.toStdString());
mainWindow->copyToClipboard(str);
}
void editorWidget::on_editorSaveButton_clicked()
{
QString str=ui->textEdit->toPlainText();
if(thisSnippet->saveSnippetToFile(str.toStdString())){
mainWindow->showAutoCloseMessageBox(this, "Success!","The snippet was saved successfully!");
}
}
void editorWidget::on_tabCloseButton_clicked()
{
mainWindow->closeTab();
}