-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnektech_doc_handler.cpp
148 lines (129 loc) · 3.61 KB
/
nektech_doc_handler.cpp
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
139
140
141
142
143
144
145
146
147
148
/*
* nektech_doc_handler.cpp - nektechTextEditor
*
* Copyright (C) NEK Tech 2013
* Developers V1.0: Pankaj Saraf
* Jitendra Khasdev
* Pooja Shukla
* Akshay Rastogi
* Poovika Ujjaini
* Copyright (C) NEK Tech 2015
*
* Author and Architect: Pankaj Saraf
*
* This program is free software; you can redistribute it and/or modify
* it under the terms and conditions of NEK Tech.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*/
#include "nektech_doc_handler.h"
#include <QtGui/QTextDocument>
#include <QtGui/QTextCursor>
#include <QtCore/QFileInfo>
NEKTech_Doc_Handler::NEKTech_Doc_Handler()
: m_target(0)
, m_doc(0)
, m_cursorPosition(-1)
, m_selectionStart(0)
, m_selectionEnd(0)
{
}
void NEKTech_Doc_Handler::setTarget(QQuickItem *target)
{
m_doc = 0;
m_target = target;
if (!m_target)
return;
QVariant doc = m_target->property("textDocument");
if (doc.canConvert<QQuickTextDocument*>()) {
QQuickTextDocument *qqdoc = doc.value<QQuickTextDocument*>();
if (qqdoc)
m_doc = qqdoc->textDocument();
}
emit targetChanged();
}
void NEKTech_Doc_Handler::setFileUrl(const QUrl &arg)
{
if (m_fileUrl != arg) {
m_fileUrl = arg;
QString fileName = QQmlFile::urlToLocalFileOrQrc(arg);
if (QFile::exists(fileName)) {
QFile file(fileName);
if (file.open(QFile::ReadOnly)) {
QByteArray data = file.readAll();
QTextCodec *codec = QTextCodec::codecForHtml(data);
setText(codec->toUnicode(data));
if (m_doc)
m_doc->setModified(false);
if (fileName.isEmpty())
m_documentTitle = QStringLiteral("untitled.txt");
else
m_documentTitle = QFileInfo(fileName).fileName();
emit textChanged();
emit documentTitleChanged();
}
}
emit fileUrlChanged();
}
}
QString NEKTech_Doc_Handler::documentTitle() const
{
return m_documentTitle;
}
void NEKTech_Doc_Handler::setDocumentTitle(QString arg)
{
if (m_documentTitle != arg) {
m_documentTitle = arg;
emit documentTitleChanged();
}
}
void NEKTech_Doc_Handler::setText(const QString &arg)
{
if (m_text != arg) {
m_text = arg;
emit textChanged();
}
}
QUrl NEKTech_Doc_Handler::fileUrl() const
{
return m_fileUrl;
}
QString NEKTech_Doc_Handler::text() const
{
return m_text;
}
void NEKTech_Doc_Handler::setCursorPosition(int position)
{
if (position == m_cursorPosition)
return;
m_cursorPosition = position;
}
QTextCursor NEKTech_Doc_Handler::textCursor() const
{
QTextCursor cursor = QTextCursor(m_doc);
if (m_selectionStart != m_selectionEnd) {
cursor.setPosition(m_selectionStart);
cursor.setPosition(m_selectionEnd, QTextCursor::KeepAnchor);
} else {
cursor.setPosition(m_cursorPosition);
}
return cursor;
}
void NEKTech_Doc_Handler::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
{
QTextCursor cursor = textCursor();
if (!cursor.hasSelection())
cursor.select(QTextCursor::WordUnderCursor);
cursor.mergeCharFormat(format);
}
void NEKTech_Doc_Handler::setSelectionStart(int position)
{
m_selectionStart = position;
}
void NEKTech_Doc_Handler::setSelectionEnd(int position)
{
m_selectionEnd = position;
}