-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnote_xml.cpp
99 lines (84 loc) · 2.21 KB
/
note_xml.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
#include "note_xml.h"
#include <QClipboard>
#include <QApplication>
#include <QTreeWidget>
#include <QDomDocument>
#include <QDomElement>
#include <QHeaderView>
#include <QDateTime>
//document
//TODO: тут модель нужна таки...
XmlNote::XmlNote(const QFileInfo& fileinfo, Note::Type type_new)
: Note(fileinfo, type_new)
{
document = new QDomDocument();
tree_widget = new QTreeWidget();
tree_widget->header()->setResizeMode(QHeaderView::ResizeToContents);
tree_widget->setColumnCount(3);
//tree_widget->setHeaderHidden(true);
// tree_widget->setHeaderLabel(title()+":");
load(); //loading note's content
}
XmlNote::~XmlNote()
{
delete tree_widget;
}
//Reading file
void XmlNote::load()
{
file.close();
if(!file.open(QIODevice::ReadOnly)) return;
if(!document->setContent(&file))
{
file.close();
return;
}
file.close();
QDomElement docElem = document->documentElement();
QDomNode n = docElem.firstChild();
while(!n.isNull())
{
QDomElement e = n.toElement(); // try to convert the node to an element.
if(!e.isNull() && e.tagName()=="task") apendTask(&e);
n = n.nextSibling();
}
}
void XmlNote::apendTask(QDomElement* element, QTreeWidgetItem* parent)
{
QTreeWidgetItem* item_text = new QTreeWidgetItem(tree_widget);
if(parent==0) tree_widget->insertTopLevelItem(0, item_text);
else parent->insertChild(0, item_text);
QDomNode n = element->firstChild();
while(!n.isNull())
{
QDomElement e = n.toElement(); // try to convert the node to an element.
if(!e.isNull())
{
if(e.tagName()=="title") item_text->setText(0, e.text());
if(e.tagName()=="task") apendTask(&e);
}
n = n.nextSibling();
}
}
//Saving note
void XmlNote::save(bool forced)
{
if(!(content_changed || forced)) return; //If file doesn't need in saving, exiting from function
// file.close();
// if(!file.open(QFile::WriteOnly | QFile::Text)) return;
// QTextStream out(&file);
// out << document->toString();
// file.close();
content_changed = false;
}
//Returning widget (it's can be placed to tabwidget)
QWidget* XmlNote::widget()
{
return tree_widget;
}
//Coping note's content to clipboard
void XmlNote::copy() const
{
QClipboard* clipboard = QApplication::clipboard();
clipboard->setText(document->toString());
}