Welcome to mirror list, hosted at ThFree Co, Russian Federation.

fb2imgs.cpp « source - github.com/lintest/fb2edit.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c70d24f0fde935bc1fd63cc984fa343f27863e88 (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#include "fb2imgs.hpp"

#include <QAbstractListModel>
#include <QBuffer>
#include <QCryptographicHash>
#include <QDialogButtonBox>
#include <QFileDialog>
#include <QFileInfo>
#include <QImageReader>
#include <QLabel>
#include <QLineEdit>
#include <QSplitter>
#include <QUrl>
#include <QVBoxLayout>
#include <QWebFrame>
#include <QTabWidget>
#include <QtDebug>

#include "fb2list.hpp"
#include "fb2page.hpp"
#include "fb2text.hpp"
#include "fb2utils.h"

static QString imgHtml(const QUrl &url)
{
    return QString("<img src=\"%1\" valign=center align=center width=100%>").arg(url.toString());
}

//---------------------------------------------------------------------------
//  FbBinary
//---------------------------------------------------------------------------

FbBinary::FbBinary(const QString &name)
    : QTemporaryFile()
    , m_name(name)
    , m_size(0)
{
}

qint64 FbBinary::write(QByteArray &data)
{
    open();
    if (m_hash.isEmpty()) m_hash = md5(data);
    m_size = QTemporaryFile::write(data);
    QBuffer buffer(&data);
    buffer.open(QIODevice::ReadOnly);
    m_type = QImageReader::imageFormat(&buffer);
    close();
    return m_size;
}

QString FbBinary::md5(const QByteArray &data)
{
    return QCryptographicHash::hash(data, QCryptographicHash::Md5).toBase64();
}

QByteArray FbBinary::data()
{
    open();
    QByteArray data = readAll();
    close();
    return data;
}

//---------------------------------------------------------------------------
//  FbStore
//---------------------------------------------------------------------------

FbStore::FbStore(QObject *parent)
    : QObject(parent)
{
}

FbStore::~FbStore()
{
    FbTemporaryIterator it(*this);
    while (it.hasNext()) delete it.next();
}

void FbStore::binary(const QString &name, const QByteArray &data)
{
    set(name, data);
}

QString FbStore::add(const QString &path, QByteArray &data)
{
    QString hash = FbBinary::md5(data);
    QString name = this->name(hash);
    if (name.isEmpty()) {
        name = newName(path);
        FbBinary * temp = new FbBinary(name);
        temp->setHash(hash);
        temp->write(data);
        append(temp);
    }
    return name;
}

QString FbStore::newName(const QString &path)
{
    QFileInfo info(path);
    QString name = info.fileName();
    if (!exists(name)) return name;
    QString base = info.baseName();
    QString suff = info.suffix();
    for (int i = 1; ; ++i) {
        QString name = QString("%1(%2).%3").arg(base).arg(i).arg(suff);
        if (exists(name)) continue;
        return name;
    }
}

FbBinary * FbStore::get(const QString &name) const
{
    FbTemporaryIterator it(*this);
    while (it.hasNext()) {
        FbBinary * file = it.next();
        if (file->name() == name) return file;
    }
    return NULL;
}

QByteArray FbStore::data(const QString &name) const
{
    FbTemporaryIterator it(*this);
    while (it.hasNext()) {
        FbBinary *file = it.next();
        if (file->name() == name) return file->data();
    }
    return QByteArray();
}

const QString & FbStore::set(const QString &name, QByteArray data, const QString &hash)
{
    FbBinary * file = get(name);
    if (!file) append(file = new FbBinary(name));
    file->setHash(hash);
    file->write(data);
    return file->hash();
}

QString FbStore::name(const QString &hash) const
{
    FbTemporaryIterator it(*this);
    while (it.hasNext()) {
        FbBinary *file = it.next();
        if (file->hash() == hash) return file->name();
    }
    return QString();
}

bool FbStore::exists(const QString &name) const
{
    FbTemporaryIterator it(*this);
    while (it.hasNext()) {
        FbBinary *file = it.next();
        if (file->name() == name) return true;
    }
    return false;
}

#if 0

//---------------------------------------------------------------------------
//  FbNetworkDiskCache
//---------------------------------------------------------------------------

QNetworkCacheMetaData FbNetworkDiskCache::metaData(const QUrl &url)
{
    qCritical() << url.toString();
    return QNetworkDiskCache::metaData(url);
}

QIODevice * FbNetworkDiskCache::data(const QUrl &url)
{
    qCritical() << url.toString();
    return QNetworkDiskCache::data(url);
}

#endif

//---------------------------------------------------------------------------
//  FbImageReply
//---------------------------------------------------------------------------

FbImageReply::FbImageReply(QNetworkAccessManager::Operation op, const QNetworkRequest &request, const QByteArray &data)
    : QNetworkReply()
    , content(data)
    , offset(0)
{
    setOperation(op);
    setRequest(request);
    setUrl(request.url());
    open(ReadOnly | Unbuffered);
    setHeader(QNetworkRequest::ContentLengthHeader, QVariant(content.size()));
    setAttribute(QNetworkRequest::CacheSaveControlAttribute, QVariant(false));
    QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection);
    QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
}

qint64 FbImageReply::readData(char *data, qint64 maxSize)
{
    if (offset >= content.size()) return -1;
    QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection);
    qint64 number = qMin(maxSize, content.size() - offset);
    memcpy(data, content.constData() + offset, number);
    offset += number;
    return number;
}

//---------------------------------------------------------------------------
//  FbNetworkAccessManager
//
//    http://doc.trolltech.com/qq/32/qq32-webkit-protocols.html
//---------------------------------------------------------------------------

FbNetworkAccessManager::FbNetworkAccessManager(QObject *parent)
    : QNetworkAccessManager(parent)
    , m_store(new FbStore(this))
{
}

void FbNetworkAccessManager::setStore(const QUrl url, FbStore *store)
{
    m_path = url.path();
    if (m_store) delete m_store;
    if (!store) store = new FbStore(this);
    store->setParent(this);
    m_store = store;
}

QNetworkReply * FbNetworkAccessManager::createRequest(Operation op, const QNetworkRequest &request, QIODevice *outgoingData)
{
    if (m_store) {
        const QUrl &url = request.url();
        const QString path = url.path();
        if (url.scheme() == "fb2" && path == m_path) {
            QString name = request.url().fragment();
            QByteArray data = m_store->data(name);
            return new FbImageReply(op, request, data);
        }
    }
    return QNetworkAccessManager::createRequest(op, request, outgoingData);
}

QVariant FbNetworkAccessManager::info(int row, int col) const
{
    if (!m_store) return QVariant();
    if (0 <= row && row < count()) {
        FbBinary *file = m_store->at(row);
        switch (col) {
            case 2: return file->type();
            case 3: return file->size();
        }
        return m_store->at(row)->name();
    }
    return QVariant();
}

QByteArray FbNetworkAccessManager::data(int index) const
{
    if (!m_store) return QByteArray();
    if (0 <= index && index < count()) {
        return m_store->at(index)->data();
    }
    return QByteArray();
}

//---------------------------------------------------------------------------
//  FbComboCtrl
//---------------------------------------------------------------------------

FbComboCtrl::FbComboCtrl(QWidget *parent)
    : QLineEdit(parent)
{
    button = new QToolButton(this);
    button->setCursor(Qt::ArrowCursor);
    button->setFocusPolicy(Qt::NoFocus);
    connect(button, SIGNAL(clicked()), SIGNAL(popup()));
    QHBoxLayout *layout = new QHBoxLayout(this);
    layout->addWidget(button, 0, Qt::AlignRight);
    layout->setSpacing(0);
    layout->setMargin(0);
}

void FbComboCtrl::resizeEvent(QResizeEvent* event)
{
    QLineEdit::resizeEvent(event);
    QMargins margins(0, 0, button->width(), 0);
    setTextMargins(margins);
}

void FbComboCtrl::setIcon(const QIcon &icon)
{
    button->setIcon(icon);
}

//---------------------------------------------------------------------------
//  FbImageDlg::FbTab
//---------------------------------------------------------------------------

FbImageDlg::FbTab::FbTab(QWidget* parent, QAbstractItemModel *model)
    : QWidget(parent)
    , combo(0)
    , edit(0)
{
    QGridLayout * layout = new QGridLayout(this);

    label = new QLabel(this);
    label->setText(tr("File name:"));
    layout->addWidget(label, 0, 0, 1, 1);

    QWidget *control;
    if (model) {
        control = combo = new QComboBox(this);
        combo->setModel(model);
    } else {
        control = edit = new FbComboCtrl(this);
    }

    QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    control->setSizePolicy(sizePolicy);
    layout->addWidget(control, 0, 1, 1, 1);

    QFrame *frame = new FbTextFrame(this);
    frame->setMinimumSize(QSize(300, 200));
    layout->addWidget(frame, 1, 0, 1, 2);

    preview = new QWebView(this);
    frame->layout()->addWidget(preview);
}

//---------------------------------------------------------------------------
//  FbImageDlg
//---------------------------------------------------------------------------

FbImageDlg::FbImageDlg(FbTextEdit *text)
    : QDialog(text)
    , owner(text)
    , tabFile(0)
    , tabPict(0)
{
    setWindowTitle(tr("Insert picture"));

    QLayout *layout = new QVBoxLayout(this);

    notebook = new QTabWidget(this);
    layout->addWidget(notebook);

    QDialogButtonBox *buttons = new QDialogButtonBox(this);
    buttons->setOrientation(Qt::Horizontal);
    buttons->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
    layout->addWidget(buttons);

    connect(buttons, SIGNAL(accepted()), SLOT(accept()));
    connect(buttons, SIGNAL(rejected()), SLOT(reject()));
    connect(notebook, SIGNAL(currentChanged(int)), SLOT(notebookChanged(int)));

    tabFile = new FbTab(notebook);
    tabFile->edit->setIcon(FbIcon("document-open"));
    tabFile->preview->setHtml(QString());
    connect(tabFile->edit, SIGNAL(textChanged(QString)), SLOT(filenameChanged(QString)));
    connect(tabFile->edit, SIGNAL(popup()), SLOT(selectFile()));
    notebook->addTab(tabFile, tr("Select file"));

    if (text->store()->count()) {
        FbImgsModel *model = new FbImgsModel(text, this);
        tabPict = new FbTab(notebook, model);
        tabPict->combo->setCurrentIndex(0);
        tabPict->preview->setHtml(QString(), text->url());
        tabPict->preview->page()->setNetworkAccessManager(text->page()->networkAccessManager());
        notebook->addTab(tabPict, tr("From collection"));
        connect(tabPict->combo, SIGNAL(activated(QString)), SLOT(pictureActivated(QString)));
    }

    tabFile->edit->setFocus();
    resize(minimumSizeHint());
}

void FbImageDlg::notebookChanged(int index)
{
    if (index) {
        disconnect(notebook, SIGNAL(currentChanged(int)), this, SLOT(notebookChanged(int)));
        if (tabPict) pictureActivated(tabPict->combo->itemText(0));
    }
}

void FbImageDlg::selectFile()
{
    QString filters;
    filters += tr("Common Graphics (*.png *.jpg *.jpeg *.gif)") += ";;";
    filters += tr("Portable Network Graphics (PNG) (*.png)") += ";;";
    filters += tr("JPEG (*.jpg *.jpeg)") += ";;";
    filters += tr("Graphics Interchange Format (*.gif)") += ";;";
    filters += tr("All Files (*)");
    QWidget *p = qobject_cast<QWidget*>(parent());
    QString path = QFileDialog::getOpenFileName(p, tr("Insert image..."), QString(), filters);
    if (path.isEmpty()) return;
    tabFile->edit->setText(path);
}

void FbImageDlg::filenameChanged(const QString & text)
{
    if (QFileInfo(text).exists()) {
        QUrl url = QUrl::fromLocalFile(text);
        preview(tabFile->preview, url);
    } else {
        tabFile->preview->setHtml(QString());
    }
}

void FbImageDlg::pictureActivated(const QString & text)
{
    QUrl url = tabPict->preview->url();
    url.setFragment(text);
    preview(tabPict->preview, url);
}

void FbImageDlg::preview(QWebView *preview, const QUrl &url)
{
    preview->setHtml(imgHtml(url), preview->url());
}

QString FbImageDlg::result() const
{
    if (tabPict && notebook->currentWidget() == tabPict) {
        return tabPict->combo->currentText();
    } else if (notebook->currentWidget() == tabFile) {
        QString path = tabFile->edit->text();
        QFile file(path);
        if (file.open(QIODevice::ReadOnly)) {
            QNetworkAccessManager *m = owner->page()->networkAccessManager();
            FbNetworkAccessManager *manager = qobject_cast<FbNetworkAccessManager*>(m);
            QByteArray data = file.readAll();
            return manager->add(path, data);
        }
    }
    return QString();
}

//---------------------------------------------------------------------------
//  FbImgsModel
//---------------------------------------------------------------------------

FbImgsModel::FbImgsModel(FbTextEdit *text, QObject *parent)
    : QAbstractListModel(parent)
{
    manager = qobject_cast<FbNetworkAccessManager*>(text->page()->networkAccessManager());
}

int FbImgsModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return 4;
}

int FbImgsModel::rowCount(const QModelIndex &parent) const
{
    return parent.isValid() ? 0 : manager->count();
}

QVariant FbImgsModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
        switch (section) {
            case 1: return tr("File name");
            case 2: return tr("Type");
            case 3: return tr("Size");
        }
    }
    return QVariant();
}

QVariant FbImgsModel::data(const QModelIndex &index, int role) const
{
    if (index.isValid()) {
        switch (role) {
            case Qt::DisplayRole: {
                return manager->info(index.row(), index.column());
            } break;
            case Qt::TextAlignmentRole: {
                switch (index.column()) {
                    case 3: return Qt::AlignRight;
                    default: return Qt::AlignLeft;
                }
            }
        }
    }
    return QVariant();
}

//---------------------------------------------------------------------------
//  FbImgsWidget
//---------------------------------------------------------------------------

FbImgsWidget::FbImgsWidget(FbTextEdit *text, QWidget* parent)
    : QWidget(parent)
    , m_text(text)
{
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);

    QSplitter *splitter = new QSplitter(Qt::Vertical, this);

    m_list = new FbListView(splitter);
    splitter->addWidget(m_list);

    FbTextFrame *frame = new FbTextFrame(splitter);
    splitter->addWidget(frame);

    m_view = new FbTextBase(frame);
    m_view->page()->setNetworkAccessManager(text->page()->networkAccessManager());
    frame->layout()->addWidget(m_view);

    splitter->setSizes(QList<int>() << 100 << 100);

    layout->addWidget(splitter);

    connect(m_text, SIGNAL(loadFinished(bool)), SLOT(loadFinished()));
    connect(m_list, SIGNAL(showCurrent(QString)), SLOT(showCurrent(QString)));
    loadFinished();
}

void FbImgsWidget::loadFinished()
{
    if (QAbstractItemModel *m = m_list->model()) m->deleteLater();
    m_view->load(QUrl());
    m_list->setModel(new FbImgsModel(m_text, this));
    m_list->reset();
    m_list->resizeColumnToContents(1);
    m_list->resizeColumnToContents(2);
    m_list->resizeColumnToContents(3);
    m_list->setColumnHidden(0, true);
}

void FbImgsWidget::showCurrent(const QString &name)
{
    QUrl url = m_text->url();
    url.setFragment(name);
    m_view->setHtml(imgHtml(url));
}