1 /* Copyright (C) 2006 J.F.Dockes
2  *   This program is free software; you can redistribute it and/or modify
3  *   it under the terms of the GNU General Public License as published by
4  *   the Free Software Foundation; either version 2 of the License, or
5  *   (at your option) any later version.
6  *
7  *   This program is distributed in the hope that it will be useful,
8  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *   GNU General Public License for more details.
11  *
12  *   You should have received a copy of the GNU General Public License
13  *   along with this program; if not, write to the
14  *   Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  */
17 #ifndef _RESTABLE_H_INCLUDED_
18 #define _RESTABLE_H_INCLUDED_
19 #include "autoconfig.h"
20 
21 #include <Qt>
22 
23 #include <string>
24 #include <map>
25 #include <vector>
26 #include <memory>
27 #include <fstream>
28 #include <functional>
29 
30 #include "ui_restable.h"
31 #include "docseq.h"
32 #include "plaintorich.h"
33 
34 class ResTable;
35 
36 typedef std::string (FieldGetter)(
37     const std::string& fldname, const Rcl::Doc& doc);
38 
39 class RecollModel : public QAbstractTableModel {
40 
41     Q_OBJECT;
42 
43 public:
44     RecollModel(const QStringList fields, ResTable *tb, QObject *parent = 0);
45 
46     // Reimplemented methods
47     virtual int rowCount (const QModelIndex& = QModelIndex()) const;
48     virtual int columnCount(const QModelIndex& = QModelIndex()) const;
49     virtual QVariant headerData (int col, Qt::Orientation orientation,
50                                  int role = Qt::DisplayRole ) const;
51     virtual QVariant data(const QModelIndex& index,
52                           int role = Qt::DisplayRole ) const;
53     virtual void saveAsCSV(std::fstream& fp);
54     virtual void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
55     // Specific methods
56     virtual void readDocSource();
57     virtual void setDocSource(std::shared_ptr<DocSequence> nsource);
getDocSource()58     virtual std::shared_ptr<DocSequence> getDocSource() {return m_source;}
59     virtual void deleteColumn(int);
getFields()60     virtual const std::vector<std::string>& getFields() {return m_fields;}
getAllFields()61     static const std::map<std::string, QString>& getAllFields() {
62         return o_displayableFields;
63     }
64     static QString displayableField(const std::string& in);
65     virtual void addColumn(int, const std::string&);
66     // Some column name are aliases/translator for base document field
67     // (ie: date, datetime->mtime). Help deal with this:
68     virtual std::string baseField(const std::string&);
69 
70     // Ignore sort() call because
setIgnoreSort(bool onoff)71     virtual void setIgnoreSort(bool onoff) {m_ignoreSort = onoff;}
72 
73     friend class ResTable;
74 
75 signals:
76     void sortDataChanged(DocSeqSortSpec);
77 
78 private:
79     ResTable *m_table{0};
80     mutable std::shared_ptr<DocSequence> m_source;
81     std::vector<std::string> m_fields;
82     std::vector<FieldGetter*> m_getters;
83     static std::map<std::string, QString> o_displayableFields;
84     bool m_ignoreSort;
85     FieldGetter* chooseGetter(const std::string&);
86     HighlightData m_hdata;
87     // Things we cache because we are repeatedly asked for the same.
88     mutable QFont m_cachedfont;
89     mutable int   m_reslfntszforcached{-1};
90     mutable Rcl::Doc m_cachedoc;
91     mutable int m_rowforcachedoc{-1};
92 };
93 
94 class ResTable;
95 
96 // Modified textBrowser for the detail area
97 class ResTableDetailArea : public QTextBrowser {
98     Q_OBJECT;
99 
100 public:
101     ResTableDetailArea(ResTable* parent = 0);
102 
103 public slots:
104     virtual void createPopupMenu(const QPoint& pos);
105     virtual void setFont();
106     virtual void init();
107 
108 private:
109     ResTable *m_table;
110 };
111 
112 
113 class ResTablePager;
114 class QUrl;
115 class RclMain;
116 class QShortcut;
117 
118 // This is an intermediary object to help setting up multiple similar
119 // shortcuts with different data (e.g. Ctrl+1, Ctrl+2 etc.). Maybe
120 // there is another way, but this one works.
121 class SCData : public QObject {
122     Q_OBJECT;
123 public:
SCData(QObject * parent,std::function<void (int)> cb,int row)124     SCData(QObject* parent, std::function<void (int)> cb, int row)
125         : QObject(parent), m_cb(cb), m_row(row) {}
126 public slots:
activate()127     virtual void activate() {
128         m_cb(m_row);
129     }
130 private:
131     std::function<void (int)>  m_cb;
132     int m_row;
133 };
134 
135 class ResTable : public QWidget, public Ui::ResTable
136 {
137     Q_OBJECT;
138 
139 public:
140     ResTable(QWidget* parent = 0)
QWidget(parent)141         : QWidget(parent) {
142             setupUi(this);
143             init();
144         }
145 
~ResTable()146     virtual ~ResTable() {}
getModel()147     virtual RecollModel *getModel() {return m_model;}
getDetailArea()148     virtual ResTableDetailArea* getDetailArea() {return m_detail;}
149     virtual int getDetailDocNumOrTopRow();
150 
151     void setRclMain(RclMain *m, bool ismain);
152     void setDefRowHeight();
153 
154 public slots:
155     virtual void onTableView_currentChanged(const QModelIndex&);
156     virtual void on_tableView_entered(const QModelIndex& index);
157     virtual void setDocSource(std::shared_ptr<DocSequence> nsource);
158     virtual void saveColState();
159     virtual void resetSource();
160     virtual void readDocSource(bool resetPos = true);
161     virtual void onSortDataChanged(DocSeqSortSpec);
162     virtual void createPopupMenu(const QPoint& pos);
163     virtual void onClicked(const QModelIndex&);
164     virtual void onDoubleClick(const QModelIndex&);
165     virtual void menuPreview();
166     virtual void menuSaveToFile();
167     virtual void menuSaveSelection();
168     virtual void menuEdit();
169     virtual void menuEditAndQuit();
170     virtual void menuOpenWith(QAction *);
171     virtual void menuCopyFN();
172     virtual void menuCopyPath();
173     virtual void menuCopyURL();
174     virtual void menuCopyText();
175     virtual void menuCopyTextAndQuit();
176     virtual void menuExpand();
177     virtual void menuPreviewParent();
178     virtual void menuOpenParent();
179     virtual void menuOpenFolder();
180     virtual void menuShowSnippets();
181     virtual void menuShowSubDocs();
182     virtual void createHeaderPopupMenu(const QPoint&);
183     virtual void deleteColumn();
184     virtual void addColumn();
185     virtual void resetSort(); // Revert to natural (relevance) order
186     virtual void saveAsCSV();
187     virtual void linkWasClicked(const QUrl&);
188     virtual void makeRowVisible(int row);
189     virtual void takeFocus();
190     virtual void onUiPrefsChanged();
191     virtual void onNewShortcuts();
192     virtual void setCurrentRowFromKbd(int row);
193     virtual void toggleHeader();
194     virtual void toggleVHeader();
195 
196 signals:
197     void docPreviewClicked(int, Rcl::Doc, int);
198     void docSaveToFileClicked(Rcl::Doc);
199     void previewRequested(Rcl::Doc);
200     void editRequested(Rcl::Doc);
201     void openWithRequested(Rcl::Doc, string cmd);
202     void headerClicked();
203     void docExpand(Rcl::Doc);
204     void showSubDocs(Rcl::Doc);
205     void showSnippets(Rcl::Doc);
206     void detailDocChanged(Rcl::Doc, std::shared_ptr<DocSequence>);
207 
208     friend class ResTablePager;
209     friend class ResTableDetailArea;
210 
211 protected:
212     bool eventFilter(QObject* obj, QEvent* event);
213 
214 private:
215     void init();
216 
217     RecollModel   *m_model{nullptr};
218     ResTablePager *m_pager{nullptr};
219     ResTableDetailArea *m_detail{nullptr};
220     int            m_detaildocnum{-1};
221     Rcl::Doc       m_detaildoc;
222     int            m_popcolumn{0};
223     RclMain *m_rclmain{nullptr};
224     bool     m_ismainres{true};
225     bool m_rowchangefromkbd{false};
226     QShortcut *m_opensc{nullptr};
227     QShortcut *m_openquitsc{nullptr};
228     QShortcut *m_previewsc{nullptr};
229     QShortcut *m_showsnipssc{nullptr};
230     QShortcut *m_showheadersc{nullptr};
231     QShortcut *m_showvheadersc{nullptr};
232     QShortcut *m_copycurtextsc{nullptr};
233     QShortcut *m_copycurtextquitsc{nullptr};
234     std::vector<SCData*> m_rowlinks;
235     std::vector<QShortcut *> m_rowsc;
236 };
237 
238 
239 #endif /* _RESTABLE_H_INCLUDED_ */
240