File: common.cpp

package info (click to toggle)
psi-plus 1.4.1456-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,188 kB
  • sloc: cpp: 211,254; ansic: 19,786; javascript: 13,687; xml: 4,056; sh: 1,610; makefile: 437; objc: 407; python: 277; ruby: 171
file content (150 lines) | stat: -rw-r--r-- 5,473 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
/*
 * common.cpp - plugin
 * Copyright (C) 2009-2010  Evgeny Khryukin
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * 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, see <https://www.gnu.org/licenses/>.
 *
 */

#include "common.h"
#include <QDir>
#include <QDomElement>
#include <QFileDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QTextEdit>
#include <QTextList>
#include <QTextStream>

//---------------------------------------------
//--------------HistoryView--------------------
//---------------------------------------------
HistoryView::HistoryView(const QString &filename, QWidget *parent) : QDialog(parent, Qt::Window)
{

    setAttribute(Qt::WA_DeleteOnClose);
    QFile file(filename);
    if (file.open(QIODevice::ReadOnly)) {
        setWindowTitle(filename.split(QDir::separator()).takeLast());
        QVBoxLayout *layout  = new QVBoxLayout(this);
        QTextEdit *  textWid = new QTextEdit();
        QString      text;
        QTextStream  in(&file);
        in.setCodec("UTF-8");
        text = in.readAll();
        textWid->setText(text);
        QTextCursor cur = textWid->textCursor();
        cur.setPosition(text.length());
        textWid->setTextCursor(cur);
        layout->addWidget(textWid);
        QPushButton *Close     = new QPushButton(tr("Close"));
        QHBoxLayout *butLayout = new QHBoxLayout();
        butLayout->addStretch();
        butLayout->addWidget(Close);
        butLayout->addStretch();
        layout->addLayout(butLayout);
        connect(Close, &QPushButton::released, this, &HistoryView::close);
        resize(800, 500);
        show();
    } else
        close();
}

//---------------------------------------------
//----------------vCardView--------------------
//---------------------------------------------
vCardView::vCardView(const QString &filename, QWidget *parent) : QDialog(parent, Qt::Window)
{

    setAttribute(Qt::WA_DeleteOnClose);
    QFile file(filename);
    if (file.open(QIODevice::ReadOnly)) {
        setWindowTitle(filename.split(QDir::separator()).takeLast());
        QVBoxLayout *layout = new QVBoxLayout(this);
        QGridLayout *main   = new QGridLayout;
        QLineEdit *  name   = new QLineEdit;
        QLineEdit *  nick   = new QLineEdit;
        QLineEdit *  birth  = new QLineEdit;
        QLineEdit *  email  = new QLineEdit;
        main->addWidget(new QLabel(tr("Full Name:")), 0, 0);
        main->addWidget(name, 0, 1);
        main->addWidget(new QLabel(tr("Nick:")), 1, 0);
        main->addWidget(nick, 1, 1);
        main->addWidget(new QLabel(tr("Birthday:")), 2, 0);
        main->addWidget(birth, 2, 1);
        main->addWidget(new QLabel(tr("Email:")), 3, 0);
        main->addWidget(email, 3, 1);
        QTextStream in(&file);
        in.setCodec("UTF-8");
        QDomDocument text;
        text.setContent(in.readAll());
        QDomElement VCard = text.documentElement();
        nick->setText(VCard.firstChildElement("NICKNAME").text());
        QString Name = VCard.firstChildElement("FN").text();
        if (Name.isEmpty()) {
            QDomElement n = VCard.firstChildElement("N");
            Name          = n.firstChildElement("FAMILY").text() + " " + n.firstChildElement("GIVEN").text();
        }
        name->setText(Name);
        birth->setText(VCard.firstChildElement("BDAY").text());
        email->setText(VCard.firstChildElement("EMAIL").firstChildElement("USERID").text());

        QPushButton *Close     = new QPushButton(tr("Close"));
        QHBoxLayout *butLayout = new QHBoxLayout();

        layout->addLayout(main);
        butLayout->addStretch();
        butLayout->addWidget(Close);
        butLayout->addStretch();
        layout->addLayout(butLayout);
        connect(Close, &QPushButton::released, this, &vCardView::close);
        setFixedSize(400, 200);
        show();
    } else
        close();
}

//---------------------------------------------
//----------------AvatarView-------------------
//---------------------------------------------
AvatarView::AvatarView(const QPixmap &pix, QWidget *parent) : QDialog(parent), pix_(pix)
{
    setAttribute(Qt::WA_DeleteOnClose);
    setWindowTitle(tr("Avatar"));
    QVBoxLayout *layout   = new QVBoxLayout(this);
    QLabel *     pixLabel = new QLabel;
    pixLabel->setPixmap(pix);
    pbSave = new QPushButton;
    pbSave->setFixedSize(25, 25);
    pbSave->setToolTip(tr("Save Image"));
    layout->addWidget(pbSave);
    layout->addWidget(pixLabel);
    connect(pbSave, &QPushButton::released, this, &AvatarView::save);
    adjustSize();
}

void AvatarView::setIcon(const QIcon &ico) { pbSave->setIcon(ico); }

void AvatarView::save()
{
    QFileDialog dialog(this);
    dialog.setModal(true);
    QString filename = dialog.getSaveFileName(this, tr("Save Avatar"), "", tr("Images (*.png *.gif *.jpg *.jpeg)"));
    if (filename.isEmpty())
        return;

    QImage image = pix_.toImage();
    image.save(filename);
}