Commit b6d2bb9c authored by Екатерина Спорова's avatar Екатерина Спорова
Browse files

Add new file

parent ec94db50
No related merge requests found
Pipeline #1234 canceled with stages
Showing with 241 additions and 0 deletions
+241 -0
tab_docs.cpp 0 → 100644
#include <QtWidgets>
#include <QtXml>
#include "tab_docs.h"
doc_tab_class::doc_tab_class(QWidget* p_parent) : QPlainTextEdit(p_parent),
_fDirectory(""),
_changed(false)
{
connect(this, &doc_tab_class::textChanged, this, &doc_tab_class::sltDocChanged);
lineNumberArea = new LineNumberArea(this);
connect(this, &doc_tab_class::blockCountChanged, this, &doc_tab_class::updateLineNumberAreaWidth);
connect(this, &doc_tab_class::updateRequest, this, &doc_tab_class::updateLineNumberArea);
connect(this, &doc_tab_class::cursorPositionChanged, this, &doc_tab_class::highlightCurrentLine);
updateLineNumberAreaWidth(0);
lineNumberArea->hide();
}
QString doc_tab_class::sltLoad()
{
_fDirectory = QFileDialog::getOpenFileName();
if (_fDirectory.isEmpty())
return "";
return sltLoad(_fDirectory);
}
void doc_tab_class::linesHelp()
{
lineNumberArea->show();
}
QString doc_tab_class::sltLoad(const QString& p_fDirectory)
{
_fDirectory = p_fDirectory;
QStringList list = p_fDirectory.split("/");
_fName = list.last();
list.clear();
list = _fName.split(".");
_fExtension = list.last();
QFile file(p_fDirectory);
if (file.open(QIODevice::ReadOnly))
{
QTextStream stream(&file);
disconnect(this, &doc_tab_class::textChanged, this, &doc_tab_class::sltDocChanged);
setPlainText(stream.readAll());
connect(this, &doc_tab_class::textChanged, this, &doc_tab_class::sltDocChanged);
qDebug() << _fName;
emit changeWidTitle("", "");
return _fName;
}
lineNumberArea->show();
return "";
}
void doc_tab_class::sltIsCodeDoc()
{
lineNumberArea->show();
highlightCurrentLine();
sltLoadHighlight();
}
void doc_tab_class::sltSave()
{
if (_fDirectory.isEmpty())
{
sltSaveAs(_fName);
return;
}
QFile file(_fDirectory);
if(file.open(QIODevice::WriteOnly))
{
QTextStream(&file) << toPlainText();
file.close();
connect(this, &doc_tab_class::textChanged, this, &doc_tab_class::sltDocChanged);
emit changeWidTitle("", "");
}
_changed = false;
}
void doc_tab_class::sltSaveAs(const QString& p_tip)
{
QString newFileName = QFileDialog::getSaveFileName(0, _fDirectory, p_tip);
if (!newFileName.isEmpty())
{
QString lastFileName = _fName;
QString lastFileDirectory = _fDirectory;
_fDirectory = newFileName;
QStringList list = _fDirectory.split("/");
_fName = list.last();
list.clear();
list = _fName.split(".");
_fExtension = list.last();
emit changeWidTitle(lastFileName, lastFileDirectory);
sltSave();
}
}
void doc_tab_class::sltLoadHighlight()
{
_highlighter = new Lighter_class(this->document(), _fExtension);
QString _styleFName = ":/hLIni/highlightTest.xml";
QFile styleFile(_styleFName);
QXmlInputSource src(&styleFile);
QXmlSimpleReader reader;
reader.setContentHandler(_highlighter);
qDebug() << "Ready to parse...";
reader.parse(src);
}
void doc_tab_class::sltDocChanged()
{
qDebug() << "Doc " << _fDirectory << " changed!";
_changed = true;
disconnect(this, &doc_tab_class::textChanged, this, &doc_tab_class::sltDocChanged);
emit fileChanded(_fName);
}
void doc_tab_class::sltSetChanged(bool p_was)
{
_changed = p_was;
}
int doc_tab_class::lineNumberAreaWidth()
{
int digits = 1;
int max = qMax(1, blockCount());
while (max >= 10) {
max /= 10;
++digits;
}
int space = 3 + fontMetrics().horizontalAdvance(QLatin1Char('9')) * digits;
return space;
}
void doc_tab_class::updateLineNumberAreaWidth(int)
{
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
}
void doc_tab_class::updateLineNumberArea(const QRect &rect, int dy)
{
if (dy)
lineNumberArea->scroll(0, dy);
else
lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
if (rect.contains(viewport()->rect()))
updateLineNumberAreaWidth(0);
}
void doc_tab_class::resizeEvent(QResizeEvent *e)
{
QPlainTextEdit::resizeEvent(e);
QRect cr = contentsRect();
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
}
void doc_tab_class::highlightCurrentLine()
{
QList<QTextEdit::ExtraSelection> extraSelections;
if (!isReadOnly())
{
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor(Qt::yellow).lighter(160);
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = textCursor();
selection.cursor.clearSelection();
extraSelections.append(selection);
}
setExtraSelections(extraSelections);
}
void doc_tab_class::linePaintEvent(QPaintEvent *event)
{
QPainter painter(lineNumberArea);
painter.fillRect(event->rect(), Qt::lightGray);
QTextBlock block = firstVisibleBlock();
int blockNumber = block.blockNumber();
int top = qRound(blockBoundingGeometry(block).translated(contentOffset()).top());
int bottom = top + qRound(blockBoundingRect(block).height());
while (block.isValid() && top <= event->rect().bottom())
{
if (block.isVisible() && bottom >= event->rect().top())
{
QString number = QString::number(blockNumber + 1);
painter.setPen(Qt::black);
painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(), Qt::AlignRight, number);
}
block = block.next();
top = bottom;
bottom = top + qRound(blockBoundingRect(block).height());
++blockNumber;
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment