source: s10k/Vago/xmlToolsInterface/xmltoolsinterfacecommandpreview.cpp@ 1194

Last change on this file since 1194 was 1093, checked in by s10k, 7 years ago

Vago 1.4

File size: 7.8 KB
Line 
1#include "xmltoolsinterfacecommandpreview.h"
2#include "ui_xmltoolsinterfacecommandpreview.h"
3
4XmlToolsInterfaceCommandPreview::XmlToolsInterfaceCommandPreview(const QString &currentFileLocation, const QString &previewFileLocation, QWidget *parent) :
5 QMainWindow(parent),
6 ui(new Ui::XmlToolsInterfaceCommandPreview)
7{
8 ui->setupUi(this);
9 this->setAttribute(Qt::WA_DeleteOnClose, true); //destroy itself once finished.
10
11 QFile currentFile(currentFileLocation);
12 currentFile.open(QIODevice::ReadOnly);
13
14 this->currentText = currentFile.readAll();
15
16 ui->teCurrentFileText->setText(this->currentText);
17
18 QFile previewFile(previewFileLocation);
19 previewFile.open(QIODevice::ReadOnly);
20
21 this->previewText = previewFile.readAll();
22
23 ui->tePreviewFileText->setText(this->previewText);
24
25 highlighterCurrentFile.setDocument(ui->teCurrentFileText->document());
26 highlighterPreviewFile.setDocument(ui->tePreviewFileText->document());
27
28 previewFile.close();
29 currentFile.close();
30
31 // Split and trim text of current and preview file
32 this->currentTextRows = this->currentText.split("\n");
33 this->previewTextRows = this->previewText.split("\n");
34
35 for(QString &currentTextRow : this->currentTextRows){
36 currentTextRow = currentTextRow.trimmed();
37 }
38 for(QString &previewTextRow : this->previewTextRows){
39 previewTextRow = previewTextRow.trimmed();
40 }
41
42
43 QString currentTextRowNumbers;
44 QString previewTextRowNumbers;
45
46 for(int i=0; i<this->currentTextRows.size(); i++){
47 currentTextRowNumbers += QString::number(i);
48 if(i < this->currentTextRows.size()-1){
49 currentTextRowNumbers += "\n";
50 }
51 }
52
53 for(int i=0; i<this->previewTextRows.size(); i++){
54 previewTextRowNumbers += QString::number(i);
55 if(i < this->previewTextRows.size()-1){
56 previewTextRowNumbers += "\n";
57 }
58 }
59
60 // Create line numbers QTextEdits
61 this->lineNumberDisplayCurrentFile = new LineNumberDisplay(ui->teCurrentFileText,this);
62 this->lineNumberDisplayPreviewFile = new LineNumberDisplay(ui->tePreviewFileText,this);
63
64 // Make line numbers background transparent (http://www.qtcentre.org/threads/12148-how-QTextEdit-transparent-to-his-parent-window)
65 this->lineNumberDisplayCurrentFile->viewport()->setAutoFillBackground(false);
66 this->lineNumberDisplayPreviewFile->viewport()->setAutoFillBackground(false);
67
68 // Add line numbers (at beginning of the horizontal layout)
69 this->ui->hlCurrentFileText->insertWidget(0,lineNumberDisplayCurrentFile);
70 this->ui->hlPreviewFileText->insertWidget(0,lineNumberDisplayPreviewFile);
71
72 // Save the default background color (OS dependent)
73 this->textEditDefaultBackgroundColor = QTextCursor(this->lineNumberDisplayCurrentFile->document()).charFormat().background();
74
75 highlightDifferences();
76
77 // Vertical scrollbars
78 connect(ui->teCurrentFileText->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(verticalScrollbarMoved(int)));
79 connect(ui->tePreviewFileText->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(verticalScrollbarMoved(int)));
80
81 // Horizontal scrollbars
82 connect(ui->teCurrentFileText->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(horizontalScrollbarMoved(int)));
83 connect(ui->tePreviewFileText->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(horizontalScrollbarMoved(int)));
84}
85
86void XmlToolsInterfaceCommandPreview::verticalScrollbarMoved(int value)
87{
88 if(ui->cbVerticalScrollbarsSynchronized->isChecked()){
89 if(ui->teCurrentFileText->verticalScrollBar()->sliderPosition() != value){
90 ui->teCurrentFileText->verticalScrollBar()->setSliderPosition(value);
91 }
92 else{
93 ui->tePreviewFileText->verticalScrollBar()->setSliderPosition(value);
94 }
95 }
96}
97
98void XmlToolsInterfaceCommandPreview::horizontalScrollbarMoved(int value)
99{
100 if(ui->cbHorizontalScrollbarsSynchronized->isChecked()){
101 if(ui->teCurrentFileText->horizontalScrollBar()->sliderPosition() != value){
102 ui->teCurrentFileText->horizontalScrollBar()->setSliderPosition(value);
103 }
104 else{
105 ui->tePreviewFileText->horizontalScrollBar()->setSliderPosition(value);
106 }
107 }
108}
109
110void XmlToolsInterfaceCommandPreview::on_cbWrapText_toggled(bool checked)
111{
112 QTextEdit::LineWrapMode wrappingMode;
113
114 if(checked){
115 wrappingMode = QTextEdit::WidgetWidth;
116 this->lineNumberDisplayCurrentFile->hide();
117 this->lineNumberDisplayPreviewFile->hide();
118 }
119 else{
120 wrappingMode = QTextEdit::NoWrap;
121 this->lineNumberDisplayCurrentFile->show();
122 this->lineNumberDisplayPreviewFile->show();
123 }
124
125 ui->teCurrentFileText->setLineWrapMode(wrappingMode);
126 ui->tePreviewFileText->setLineWrapMode(wrappingMode);
127}
128
129void XmlToolsInterfaceCommandPreview::highlightDifferences(){
130
131 QTextCharFormat fmt;
132 QTextCursor cursor;
133
134 for(int i=0; i<currentTextRows.size(); i++){
135 if(i < this->previewTextRows.size()){
136 if(this->currentTextRows.at(i) != this->previewTextRows.at(i)){
137 fmt.setBackground(QColor(0xFFC864)); // orange color
138
139 cursor = QTextCursor(ui->teCurrentFileText->document());
140 cursor.setPosition(ui->teCurrentFileText->document()->findBlockByLineNumber(i).position());
141 cursor.select(QTextCursor::LineUnderCursor);
142 cursor.setCharFormat(fmt);
143
144 fmt.setBackground(Qt::yellow);
145
146 cursor = QTextCursor(ui->tePreviewFileText->document());
147 cursor.setPosition(ui->tePreviewFileText->document()->findBlockByLineNumber(i).position());
148 cursor.select(QTextCursor::LineUnderCursor);
149 cursor.setCharFormat(fmt);
150 }
151 }
152 else{ // if we have more rows in the current file than in the preview file we highlight the the extra rows in the current file
153 fmt.setBackground(QColor(0xFFC864)); // orange color
154
155 QTextCursor cursor(ui->teCurrentFileText->document());
156 cursor.setPosition(ui->teCurrentFileText->document()->findBlockByLineNumber(i).position());
157 cursor.select(QTextCursor::LineUnderCursor);
158 cursor.setCharFormat(fmt);
159 }
160 }
161
162 // if we have less rows in the current file than in the preview file we highlight the the extra rows in the preview file
163 if(this->currentTextRows.size() < this->previewTextRows.size()){
164 for(int i=this->currentTextRows.size(); i<this->previewTextRows.size(); i++){
165 fmt.setBackground(Qt::yellow);
166
167 cursor = QTextCursor(ui->tePreviewFileText->document());
168 cursor.setPosition(ui->tePreviewFileText->document()->findBlockByLineNumber(i).position());
169 cursor.select(QTextCursor::LineUnderCursor);
170 cursor.setCharFormat(fmt);
171 }
172 }
173
174 highlighterPreviewFile.rehighlight();
175 highlighterCurrentFile.rehighlight();
176}
177
178XmlToolsInterfaceCommandPreview::~XmlToolsInterfaceCommandPreview()
179{
180 delete this->lineNumberDisplayCurrentFile;
181 delete this->lineNumberDisplayPreviewFile;
182 delete this->ui;
183}
184
185
186
187void XmlToolsInterfaceCommandPreview::on_checkBox_toggled(bool checked)
188{
189 if(checked){
190 highlightDifferences();
191 }
192 else{
193 QTextCursor cursor(ui->teCurrentFileText->document());
194 QTextCharFormat fmt;
195 fmt.setBackground(this->textEditDefaultBackgroundColor);
196
197 cursor.select(QTextCursor::Document);
198 cursor.setCharFormat(fmt);
199
200 cursor = QTextCursor(ui->tePreviewFileText->document());
201 cursor.select(QTextCursor::Document);
202 cursor.setCharFormat(fmt);
203 }
204}
Note: See TracBrowser for help on using the repository browser.