~adamreichold/qpdfview/trunk

« back to all changes in this revision

Viewing changes to sources/djvumodel.cpp

  • Committer: Adam Reichold
  • Date: 2014-03-29 10:20:32 UTC
  • Revision ID: adam.reichold@t-online.com-20140329102032-6z5yl9a2fkwm62rd
Make proper use of application and anonymous namespaces and fix a few header guards.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#include <libdjvu/ddjvuapi.h>
33
33
#include <libdjvu/miniexp.h>
34
34
 
35
 
static void clearMessageQueue(ddjvu_context_t* context, bool wait)
 
35
namespace
 
36
{
 
37
 
 
38
void clearMessageQueue(ddjvu_context_t* context, bool wait)
36
39
{
37
40
    if(wait)
38
41
    {
52
55
    }
53
56
}
54
57
 
55
 
static void waitForMessageTag(ddjvu_context_t* context, ddjvu_message_tag_t tag)
 
58
void waitForMessageTag(ddjvu_context_t* context, ddjvu_message_tag_t tag)
56
59
{
57
60
    ddjvu_message_wait(context);
58
61
 
76
79
    }
77
80
}
78
81
 
79
 
Model::DjVuPage::DjVuPage(const DjVuDocument* parent, int index, const ddjvu_pageinfo_t& pageinfo) :
 
82
QString loadText(miniexp_t textExp, const QRect& rect, int pageHeight)
 
83
{
 
84
    const int textLength = miniexp_length(textExp);
 
85
 
 
86
    if(textLength >= 6 && miniexp_symbolp(miniexp_nth(0, textExp)))
 
87
    {
 
88
        const int xmin = miniexp_to_int(miniexp_nth(1, textExp));
 
89
        const int ymin = miniexp_to_int(miniexp_nth(2, textExp));
 
90
        const int xmax = miniexp_to_int(miniexp_nth(3, textExp));
 
91
        const int ymax = miniexp_to_int(miniexp_nth(4, textExp));
 
92
 
 
93
        if(rect.intersects(QRect(xmin, pageHeight - ymax, xmax - xmin, ymax - ymin)))
 
94
        {
 
95
            if(qstrncmp(miniexp_to_name(miniexp_nth(0, textExp)), "word", 4) == 0)
 
96
            {
 
97
                return QString::fromUtf8(miniexp_to_str(miniexp_nth(5, textExp)));
 
98
            }
 
99
            else
 
100
            {
 
101
                QStringList text;
 
102
 
 
103
                for(int textN = 5; textN < textLength; ++textN)
 
104
                {
 
105
                    text.append(loadText(miniexp_nth(textN, textExp), rect, pageHeight));
 
106
                }
 
107
 
 
108
                if(qstrncmp(miniexp_to_name(miniexp_nth(0, textExp)), "line", 4) == 0)
 
109
                {
 
110
                    return text.join(" ");
 
111
                }
 
112
                else
 
113
                {
 
114
                    return text.join("\n");
 
115
                }
 
116
            }
 
117
        }
 
118
    }
 
119
 
 
120
    return QString();
 
121
}
 
122
 
 
123
void loadOutline(miniexp_t outlineExp, int offset, QStandardItem* parent, const QHash< QString, int >& indexByName)
 
124
{
 
125
    const int outlineLength = miniexp_length(outlineExp);
 
126
 
 
127
    for(int outlineN = qMax(0, offset); outlineN < outlineLength; ++outlineN)
 
128
    {
 
129
        miniexp_t bookmarkExp = miniexp_nth(outlineN, outlineExp);
 
130
        const int bookmarkLength = miniexp_length(bookmarkExp);
 
131
 
 
132
        if(bookmarkLength <= 1 || !miniexp_stringp(miniexp_nth(0, bookmarkExp)) || !miniexp_stringp(miniexp_nth(1, bookmarkExp)))
 
133
        {
 
134
            continue;
 
135
        }
 
136
 
 
137
        const QString title = QString::fromUtf8(miniexp_to_str(miniexp_nth(0, bookmarkExp)));
 
138
        QString destination = QString::fromUtf8(miniexp_to_str(miniexp_nth(1, bookmarkExp)));
 
139
 
 
140
        if(!title.isEmpty() && !destination.isEmpty())
 
141
        {
 
142
            if(destination.at(0) == QLatin1Char('#'))
 
143
            {
 
144
                destination.remove(0,1);
 
145
 
 
146
                bool ok = false;
 
147
                int destinationPage = destination.toInt(&ok);
 
148
 
 
149
                if(!ok)
 
150
                {
 
151
                    if(indexByName.contains(destination))
 
152
                    {
 
153
                        destinationPage = indexByName[destination] + 1;
 
154
                    }
 
155
                    else
 
156
                    {
 
157
                        continue;
 
158
                    }
 
159
                }
 
160
 
 
161
                QStandardItem* item = new QStandardItem(title);
 
162
                item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
 
163
 
 
164
                item->setData(destinationPage, Qt::UserRole + 1);
 
165
 
 
166
                QStandardItem* pageItem = item->clone();
 
167
                pageItem->setText(QString::number(destinationPage));
 
168
                pageItem->setTextAlignment(Qt::AlignRight);
 
169
 
 
170
                parent->appendRow(QList< QStandardItem* >() << item << pageItem);
 
171
 
 
172
                if(bookmarkLength >= 3)
 
173
                {
 
174
                    loadOutline(bookmarkExp, 2, item, indexByName);
 
175
                }
 
176
            }
 
177
        }
 
178
    }
 
179
}
 
180
 
 
181
} // anonymous
 
182
 
 
183
namespace qpdfview
 
184
{
 
185
 
 
186
model::DjVuPage::DjVuPage(const DjVuDocument* parent, int index, const ddjvu_pageinfo_t& pageinfo) :
80
187
    m_parent(parent),
81
188
    m_index(index),
82
189
    m_size(pageinfo.width, pageinfo.height),
84
191
{
85
192
}
86
193
 
87
 
Model::DjVuPage::~DjVuPage()
 
194
model::DjVuPage::~DjVuPage()
88
195
{
89
196
}
90
197
 
91
 
QSizeF Model::DjVuPage::size() const
 
198
QSizeF model::DjVuPage::size() const
92
199
{
93
200
    return 72.0 / m_resolution * m_size;
94
201
}
95
202
 
96
 
QImage Model::DjVuPage::render(qreal horizontalResolution, qreal verticalResolution, Rotation rotation, const QRect& boundingRect) const
 
203
QImage model::DjVuPage::render(qreal horizontalResolution, qreal verticalResolution, Rotation rotation, const QRect& boundingRect) const
97
204
{
98
205
    QMutexLocker mutexLocker(&m_parent->m_mutex);
99
206
 
194
301
    return image;
195
302
}
196
303
 
197
 
QList< Model::Link* > Model::DjVuPage::links() const
 
304
QList< model::Link* > model::DjVuPage::links() const
198
305
{
199
306
    QMutexLocker mutexLocker(&m_parent->m_mutex);
200
307
 
336
443
    return links;
337
444
}
338
445
 
339
 
static QString loadText(miniexp_t textExp, const QRect& rect, int pageHeight)
340
 
{
341
 
    const int textLength = miniexp_length(textExp);
342
 
 
343
 
    if(textLength >= 6 && miniexp_symbolp(miniexp_nth(0, textExp)))
344
 
    {
345
 
        const int xmin = miniexp_to_int(miniexp_nth(1, textExp));
346
 
        const int ymin = miniexp_to_int(miniexp_nth(2, textExp));
347
 
        const int xmax = miniexp_to_int(miniexp_nth(3, textExp));
348
 
        const int ymax = miniexp_to_int(miniexp_nth(4, textExp));
349
 
 
350
 
        if(rect.intersects(QRect(xmin, pageHeight - ymax, xmax - xmin, ymax - ymin)))
351
 
        {
352
 
            if(qstrncmp(miniexp_to_name(miniexp_nth(0, textExp)), "word", 4) == 0)
353
 
            {
354
 
                return QString::fromUtf8(miniexp_to_str(miniexp_nth(5, textExp)));
355
 
            }
356
 
            else
357
 
            {
358
 
                QStringList text;
359
 
 
360
 
                for(int textN = 5; textN < textLength; ++textN)
361
 
                {
362
 
                    text.append(loadText(miniexp_nth(textN, textExp), rect, pageHeight));
363
 
                }
364
 
 
365
 
                if(qstrncmp(miniexp_to_name(miniexp_nth(0, textExp)), "line", 4) == 0)
366
 
                {
367
 
                    return text.join(" ");
368
 
                }
369
 
                else
370
 
                {
371
 
                    return text.join("\n");
372
 
                }
373
 
            }
374
 
        }
375
 
    }
376
 
 
377
 
    return QString();
378
 
}
379
 
 
380
 
QString Model::DjVuPage::text(const QRectF& rect) const
 
446
QString model::DjVuPage::text(const QRectF& rect) const
381
447
{
382
448
    QMutexLocker mutexLocker(&m_parent->m_mutex);
383
449
 
404
470
    return text.trimmed();
405
471
}
406
472
 
407
 
QList< QRectF > Model::DjVuPage::search(const QString& text, bool matchCase) const
 
473
QList< QRectF > model::DjVuPage::search(const QString& text, bool matchCase) const
408
474
{
409
475
    QMutexLocker mutexLocker(&m_parent->m_mutex);
410
476
 
496
562
    return results;
497
563
}
498
564
 
499
 
Model::DjVuDocument::DjVuDocument(ddjvu_context_t* context, ddjvu_document_t* document) :
 
565
model::DjVuDocument::DjVuDocument(ddjvu_context_t* context, ddjvu_document_t* document) :
500
566
    m_mutex(),
501
567
    m_context(context),
502
568
    m_document(document),
524
590
    }
525
591
}
526
592
 
527
 
Model::DjVuDocument::~DjVuDocument()
 
593
model::DjVuDocument::~DjVuDocument()
528
594
{
529
595
    ddjvu_document_release(m_document);
530
596
    ddjvu_context_release(m_context);
531
597
    ddjvu_format_release(m_format);
532
598
}
533
599
 
534
 
int Model::DjVuDocument::numberOfPages() const
 
600
int model::DjVuDocument::numberOfPages() const
535
601
{
536
602
    QMutexLocker mutexLocker(&m_mutex);
537
603
 
538
604
    return ddjvu_document_get_pagenum(m_document);
539
605
}
540
606
 
541
 
Model::Page* Model::DjVuDocument::page(int index) const
 
607
model::Page* model::DjVuDocument::page(int index) const
542
608
{
543
609
    QMutexLocker mutexLocker(&m_mutex);
544
610
 
567
633
    return new DjVuPage(this, index, pageinfo);
568
634
}
569
635
 
570
 
QStringList Model::DjVuDocument::saveFilter() const
 
636
QStringList model::DjVuDocument::saveFilter() const
571
637
{
572
638
    return QStringList() << "DjVu (*.djvu *.djv)";
573
639
}
574
640
 
575
 
bool Model::DjVuDocument::canSave() const
 
641
bool model::DjVuDocument::canSave() const
576
642
{
577
643
    return true;
578
644
}
579
645
 
580
 
bool Model::DjVuDocument::save(const QString& filePath, bool withChanges) const
 
646
bool model::DjVuDocument::save(const QString& filePath, bool withChanges) const
581
647
{
582
648
    Q_UNUSED(withChanges);
583
649
 
602
668
    return !ddjvu_job_error(job);
603
669
}
604
670
 
605
 
static void loadOutline(miniexp_t outlineExp, int offset, QStandardItem* parent, const QHash< QString, int >& indexByName)
606
 
{
607
 
    const int outlineLength = miniexp_length(outlineExp);
608
 
 
609
 
    for(int outlineN = qMax(0, offset); outlineN < outlineLength; ++outlineN)
610
 
    {
611
 
        miniexp_t bookmarkExp = miniexp_nth(outlineN, outlineExp);
612
 
        const int bookmarkLength = miniexp_length(bookmarkExp);
613
 
 
614
 
        if(bookmarkLength <= 1 || !miniexp_stringp(miniexp_nth(0, bookmarkExp)) || !miniexp_stringp(miniexp_nth(1, bookmarkExp)))
615
 
        {
616
 
            continue;
617
 
        }
618
 
 
619
 
        const QString title = QString::fromUtf8(miniexp_to_str(miniexp_nth(0, bookmarkExp)));
620
 
        QString destination = QString::fromUtf8(miniexp_to_str(miniexp_nth(1, bookmarkExp)));
621
 
 
622
 
        if(!title.isEmpty() && !destination.isEmpty())
623
 
        {
624
 
            if(destination.at(0) == QLatin1Char('#'))
625
 
            {
626
 
                destination.remove(0,1);
627
 
 
628
 
                bool ok = false;
629
 
                int destinationPage = destination.toInt(&ok);
630
 
 
631
 
                if(!ok)
632
 
                {
633
 
                    if(indexByName.contains(destination))
634
 
                    {
635
 
                        destinationPage = indexByName[destination] + 1;
636
 
                    }
637
 
                    else
638
 
                    {
639
 
                        continue;
640
 
                    }
641
 
                }
642
 
 
643
 
                QStandardItem* item = new QStandardItem(title);
644
 
                item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
645
 
 
646
 
                item->setData(destinationPage, Qt::UserRole + 1);
647
 
 
648
 
                QStandardItem* pageItem = item->clone();
649
 
                pageItem->setText(QString::number(destinationPage));
650
 
                pageItem->setTextAlignment(Qt::AlignRight);
651
 
 
652
 
                parent->appendRow(QList< QStandardItem* >() << item << pageItem);
653
 
 
654
 
                if(bookmarkLength >= 3)
655
 
                {
656
 
                    loadOutline(bookmarkExp, 2, item, indexByName);
657
 
                }
658
 
            }
659
 
        }
660
 
    }
661
 
}
662
 
 
663
 
void Model::DjVuDocument::loadOutline(QStandardItemModel* outlineModel) const
 
671
void model::DjVuDocument::loadOutline(QStandardItemModel* outlineModel) const
664
672
{
665
673
    Document::loadOutline(outlineModel);
666
674
 
697
705
    ddjvu_miniexp_release(m_document, outlineExp);
698
706
}
699
707
 
700
 
void Model::DjVuDocument::loadProperties(QStandardItemModel* propertiesModel) const
 
708
void model::DjVuDocument::loadProperties(QStandardItemModel* propertiesModel) const
701
709
{
702
710
    Document::loadProperties(propertiesModel);
703
711
 
760
768
    setObjectName("DjVuPlugin");
761
769
}
762
770
 
763
 
Model::Document* DjVuPlugin::loadDocument(const QString& filePath) const
 
771
model::Document* DjVuPlugin::loadDocument(const QString& filePath) const
764
772
{
765
773
    ddjvu_context_t* context = ddjvu_context_create("qpdfview");
766
774
    ddjvu_document_t* document = ddjvu_document_create_by_filename(context, QFile::encodeName(filePath), FALSE);
782
790
        return 0;
783
791
    }
784
792
 
785
 
    return new Model::DjVuDocument(context, document);
 
793
    return new model::DjVuDocument(context, document);
786
794
}
787
795
 
 
796
} // qpdfview
 
797
 
788
798
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
789
799
 
790
 
Q_EXPORT_PLUGIN2(qpdfview_djvu, DjVuPlugin)
 
800
Q_EXPORT_PLUGIN2(qpdfview_djvu, qpdfview::DjVuPlugin)
791
801
 
792
802
#endif // QT_VERSION