~sachinr/xpad/xpad-qt

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
#include "pad.h"
#include <QtCore>
#include <iostream>
#include <fstream>
#include <string>
#include <QFile>

Pad::Pad()
{
    padwindow.SetPad(this);
    padwindow.show();
}

// close clicked handler
void Pad::dataReceived(QString data)
{
    windowBuffer = data;
    saveToFile();
}

void Pad::show()
{
    padwindow.show();
}

void Pad::setFilename(QString file)
{
    filename = file;
}

void Pad::loadFromFile()
{
    QFile file(filename);
    file.open(QIODevice::ReadOnly);
    QTextStream in(&file);
    QString contents;
    while(!in.atEnd())
    {
        QString line = in.readLine();
        contents.append(line);
        contents.append('\n');
    }
    windowBuffer = contents;
    padwindow.SetText(windowBuffer);
}

void Pad::saveToFile()
{
    // wofstream doesn't work in qt
//    wstring f;
//    wofstream file;
//    file.open(f.c_str()); // error
//    file << windowBuffer;
//    file.close();

    auto current_buffer = padwindow.GetText();
    if (current_buffer == windowBuffer)
    {
        return;
    }
    windowBuffer = current_buffer;

    QFile file(filename);
    if (file.open(QIODevice::WriteOnly))
    {
        QTextStream stream(&file);
        stream << windowBuffer << endl;
    }
}

Pad::~Pad()
{
}