1 /* This file is part of the KDE project
2  * Copyright (C) 2011 Ko GmbH <cbo@kogmbh.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 #ifndef INDEXGENERATORMANAGER_H
20 #define INDEXGENERATORMANAGER_H
21 
22 
23 #include <QObject>
24 #include <QMetaType>
25 #include <QHash>
26 #include <QTimer>
27 
28 class QTextDocument;
29 class KoTextDocumentLayout;
30 class KoTableOfContentsGeneratorInfo;
31 class ToCGenerator;
32 
33 class IndexGeneratorManager : public QObject
34 {
35     Q_OBJECT
36 private:
37     explicit IndexGeneratorManager(QTextDocument *document);
38 public:
39     ~IndexGeneratorManager() override;
40 
41     static IndexGeneratorManager *instance(QTextDocument *document);
42 
43     bool generate();
44 
45 public Q_SLOTS:
46     void requestGeneration();
47     void startDoneTimer();
48 
49 private Q_SLOTS:
50     void layoutDone();
51     void timeout();
52 
53 private:
54     enum State {
55         Resting,  // We are not doing anything, and don't need to either
56         FirstRunNeeded, // We would like to update the indexes, with dummy pg nums
57         FirstRun, // Updating indexes, so prevent layout and ignore documentChanged()
58         FirstRunLayouting, // KoTextDocumentLayout is layouting so sit still
59         SecondRunNeeded, // Would like to update the indexes, getting pg nums right
60         SecondRun, // Updating indexes, so prevent layout and ignore documentChanged()
61         SecondRunLayouting // KoTextDocumentLayout is layouting so sit still
62     };
63     QTextDocument *m_document;
64     KoTextDocumentLayout *m_documentLayout;
65     QHash<KoTableOfContentsGeneratorInfo *, ToCGenerator *> m_generators;
66     State m_state;
67     QTimer m_updateTimer;
68     QTimer m_doneTimer;
69 };
70 
71 Q_DECLARE_METATYPE(IndexGeneratorManager *)
72 
73 #endif
74