File: mainwindow.cpp

package info (click to toggle)
kf6-ktexttemplate 6.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,272 kB
  • sloc: cpp: 19,464; javascript: 6,043; python: 297; ruby: 24; makefile: 5
file content (158 lines) | stat: -rw-r--r-- 5,266 bytes parent folder | download | duplicates (2)
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
149
150
151
152
153
154
155
156
157
158
/*
  This file is part of the KTextTemplate library

  SPDX-FileCopyrightText: 2010 Stephen Kelly <steveire@gmail.com>

  SPDX-License-Identifier: LGPL-2.1-or-later

*/

#include "mainwindow.h"
#include <QDateTime>
#include <QDebug>
#include <QSplitter>
#include <QVBoxLayout>

#include "contact.h"
#include <QComboBox>
#include <QDir>
#include <QTimer>

#include <KTextTemplate/Context>
#include <KTextTemplate/Engine>
#include <KTextTemplate/FileSystemTemplateLoader>

#include "ktexttemplate_paths.h"

static const struct {
    const char *name;
    const char *email;
    const char *phone;
    int houseNumber;
    const char *street;
    const char *city;
    const char *nickname;
    const char *salaryCurrency;
    double salary;
    double rating;
    int daysOld;
} contacts[] = {{"Alice Murphy", "alice@wonderland.com", "+12345", 123, "Fake St", "New York", "dreamer", "EUR", 45000.45, 234.4, 7500},
                {"Bob Murphy", "bob@murphy.com", "+34567", 456, "Fake St", "Paris", "bobby", "EUR", 560000.56, 2340.4, 8500},
                {"Carly Carlson", "carly@carlson.com", "+56789", 123, "No St", "London", "carlie", "USD", 67000.67, 23400.4, 8000},
                {"David Doyle", "david@doyle.com", "+77894", 456, "No St", "Berlin", "doyler", "USD", 78000.78, 234000.4, 7575},
                {"Emma Stevenson", "emma@stevenson.com", "+65432", 89, "Fake St", "Syndey", "emms", "EUR", 5400000.54, 2340000.4, 8080},
                {"Frank Fischer", "frank@fischer.com", "+54321", 321, "Fake St", "Tokyo", "fish", "EUR", 89000.89, 234000.4, 8754},
                {"Gina Jameson", "gina@jameson.com", "+98765", 123, "Other St", "Beijing", "genes", "EUR", 430000, 23400000.4, 9000}};

MainWindow::MainWindow(const QString &templateDir, QWidget *parent)
    : QWidget(parent)
{
    QHBoxLayout *layout = new QHBoxLayout(this);
    QSplitter *splitter = new QSplitter(this);
    layout->addWidget(splitter);

    m_list = new QListWidget(splitter);
    m_list->setSelectionMode(QAbstractItemView::ExtendedSelection);

    const int numContacts = sizeof(contacts) / sizeof(*contacts);

    QList<Contact *> contactList;
    for (int i = 0; i < numContacts; ++i) {
        Contact *c = new Contact(this);
        c->setName(contacts[i].name);
        c->setEmail(contacts[i].email);
        c->setPhone(contacts[i].phone);
        Address *address = new Address(c);
        address->setHouseNumber(contacts[i].houseNumber);
        address->setStreetName(contacts[i].street);
        address->setCity(contacts[i].city);
        c->setAddress(address);
        c->setNickname(contacts[i].nickname);
        c->setSalaryCurrency(contacts[i].salaryCurrency);
        c->setSalary(contacts[i].salary);
        c->setRating(contacts[i].rating);
        c->setBirthday(QDate::currentDate().addDays(-contacts[i].daysOld));
        contactList.append(c);
    }

    for (int i = 0; i < numContacts; ++i) {
        Contact *friend1 = contactList[(i + 1) % numContacts];
        Contact *friend2 = contactList[(i + 2) % numContacts];
        Contact *friend3 = contactList[(i + 3) % numContacts];
        QList<QObject *> friends = QList<QObject *>() << friend1 << friend2 << friend3;
        contactList[i]->setFriends(friends);
    }

    for (Contact *contact : contactList) {
        m_list->addItem(contact);
    }

    connect(m_list->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SLOT(render()));

    QWidget *widget = new QWidget(splitter);
    QVBoxLayout *wLayout = new QVBoxLayout();
    widget->setLayout(wLayout);

    m_combo = new QComboBox;
    wLayout->addWidget(m_combo);

    m_templateDir = templateDir;
    QDir themeDir(templateDir);
    m_combo->insertItems(0, themeDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot));

    connect(m_combo, SIGNAL(currentIndexChanged(int)), SLOT(render()));

    m_webView = new QWebEngineView;
    wLayout->addWidget(m_webView);
    QTimer::singleShot(0, this, SLOT(delayedInit()));
}

void MainWindow::delayedInit()
{
    m_engine = new KTextTemplate::Engine(this);
    m_engine->addDefaultLibrary("ktexttemplate_i18n");
    m_engine->addDefaultLibrary("ktexttemplate_scriptabletags");

    initLocalizer();

    m_templateLoader = QSharedPointer<KTextTemplate::FileSystemTemplateLoader>(new KTextTemplate::FileSystemTemplateLoader(m_localizer));
    m_templateLoader->setTemplateDirs(QStringList() << m_templateDir);

    m_engine->addTemplateLoader(m_templateLoader);
}

void MainWindow::initLocalizer()
{
}

void MainWindow::render() const
{
    const QList<QListWidgetItem *> list = m_list->selectedItems();
    QVariantList contacts;

    for (QListWidgetItem *item : list)
        contacts << QVariant::fromValue(static_cast<QObject *>(static_cast<Contact *>(item)));

    m_templateLoader->setTheme(m_combo->currentText());
    KTextTemplate::Template t = m_engine->loadByName("main.html");

    if (t->error()) {
        qDebug() << t->errorString();
        return;
    }

    KTextTemplate::Context c;
    c.setLocalizer(m_localizer);
    c.insert("contacts", contacts);

    const QString result = t->render(&c);

    if (t->error()) {
        qDebug() << t->errorString();
        return;
    }

    m_webView->setHtml(result);
}

#include "moc_mainwindow.cpp"