File size: 2,014 Bytes
a5ffdcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*
**********************************************************************************
**
** LibreCAD is a cross-platform 2D CAD program (github.com/LibreCAD/LibreCAD).
**
** Copyright (C) 2016 ravas (github.com/r-a-v-a-s)
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License Version 2
** as published by the Free Software Foundation.
**
** 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.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**
** http://www.gnu.org/licenses/gpl-2.0.html
**
**********************************************************************************
*/

#include <QFile>

#include "textfileviewer.h"
#include "ui_textfileviewer.h"

TextFileViewer::TextFileViewer(QWidget* parent) :
    QFrame(parent),
    ui(new Ui::TextFileViewer)
{
    ui->setupUi(this);
    ui->text_edit->setReadOnly(true);

    connect(ui->list, &QListWidget::itemClicked, this, &TextFileViewer::loadFile);
}

TextFileViewer::~TextFileViewer()
{
    delete ui;
}

bool TextFileViewer::addFile(QString name, QString path)
{
    QFile file(path);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return false;

    QTextStream txt_stream(&file);
    auto txt = txt_stream.readAll();

    auto item = new QListWidgetItem(name, ui->list);
    item->setWhatsThis(txt);
    return true;
}

void TextFileViewer::loadFile(QListWidgetItem* item)
{
    ui->text_edit->setPlainText(item->whatsThis());
}

void TextFileViewer::setFile(QString name)
{
    auto item = ui->list->findItems(name, Qt::MatchExactly)[0];
    ui->list->setCurrentItem(item);
    loadFile(item);
}