File indexing completed on 2024-12-22 04:18:17

0001 /***************************************************************************
0002  *                                                                         *
0003  *   copyright : (C) 2011 Joshua Netterfield                               *
0004  *                   joshua.netterfield@gmail.com                          *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  ***************************************************************************/
0012 
0013 #include <QCompleter>
0014 #include <QStringList>
0015 #include <QAbstractTableModel>
0016 #include <QTableView>
0017 #include <QDebug>
0018 #include "cclineedit.h"
0019 
0020 #ifndef CCLINEEDIT_P_H
0021 #define CCLINEEDIT_P_H
0022 
0023 class QPushButton;
0024 
0025 namespace Kst {
0026 
0027 class CCTableModel;
0028 class CCTableView;
0029 
0030 class CategoricalCompleter : public QCompleter {
0031     Q_OBJECT
0032     QList<CompletionCase> _data;
0033     CCTableView* _tableView;
0034     CompletionCase* _currentSubset;
0035 public:
0036     friend class CCCommonEdit;
0037     friend class CCLineEdit;
0038     friend class CCTextEdit;
0039     friend class SVCCLineEdit;
0040     friend class SVCCTextEdit;
0041     /**
0042    * Creates an autocompleter for child.
0043    *
0044    * Each QStringList inside "data" represents a column.
0045    * The first element of each column is the column title (category)
0046    * The remaining elements are items under that category.
0047    *
0048    * CategoricalCompleter does not take ownership of data.
0049    */
0050     CategoricalCompleter(QLineEdit *lineEdit, QList<CompletionCase> data);
0051     CategoricalCompleter(QTextEdit *textEdit, QList<CompletionCase> data);
0052     ~CategoricalCompleter();
0053 
0054 protected:
0055     bool eventFilter(QObject *o, QEvent *e);
0056     //  QStringList splitPath(const QString &path) const;
0057     //  QString pathFromIndex(const QModelIndex &index) const;
0058     static QStringList join(CompletionCase&,QString prefix="",QStringList searchpattern=QStringList(),int complength=0);
0059     static QStringList getDefault(QList<CompletionCase>&);
0060 public slots:
0061     void verifyPrefix();
0062 };
0063 
0064 class CCTableModel : public QAbstractTableModel {
0065     Q_OBJECT
0066     QList<QStringList> _visibleData;
0067     QSize s_minSizeCache[32];
0068 public:
0069     explicit CCTableModel(const QList<QStringList>& visibleData) : _visibleData(visibleData)
0070     {
0071         for(int i=0;i<32;i++) {
0072             s_minSizeCache[i]=QSize(-1,-1);
0073         }
0074     }
0075 
0076     int rowCount(const QModelIndex &parent) const;
0077     int columnCount(const QModelIndex &parent) const;
0078     QVariant data(const QModelIndex &index, int role) const;
0079     Qt::ItemFlags flags(const QModelIndex &index) const;
0080     QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
0081 signals:
0082     void checkSize() const;
0083 };
0084 
0085 class CCTableView : public QTableView {
0086     Q_OBJECT
0087     CompletionCase* _data;
0088     QAbstractItemModel* origModel;
0089     CategoricalCompleter* completer;
0090     CCLineEdit* _le;
0091     CCTextEdit* _te;
0092     QString _prefix;
0093     bool _goingRight;
0094 #if defined(__QNX__) || defined(__ANDROID__)
0095     QPushButton* _close;
0096 #endif
0097 
0098 public:
0099     friend class CCLineEdit;
0100     friend class CCTextEdit;
0101     friend class CategoricalCompleter;
0102 
0103     explicit CCTableView(CompletionCase* _data);
0104     const QString& prefix() { return _prefix; }
0105 
0106 protected:
0107     void keyPressEvent(QKeyEvent *event);
0108     void mousePressEvent(QMouseEvent *event);
0109     void showEvent(QShowEvent *);
0110     void resizeEvent(QResizeEvent *event);
0111     int widgetCursorPosition() {
0112         if(_le) {
0113             return _le->cursorPosition();
0114         } else if (_te) {
0115             return _te->textCursor().anchor();
0116         } else {
0117             qDebug()<<"CCTableView::widgetCursorPosition(): invalid widget";
0118             return -1;
0119         }
0120     }
0121     QString widgetText() {
0122         if(_le) {
0123             return _le->text();
0124         } else if (_te) {
0125             return _te->toPlainText();
0126         } else {
0127             qDebug()<<"CCTableView::widgetText(): invalid widget";
0128             return "CCTableView::widgetText(): invalid widget";
0129         }
0130     }
0131 
0132 public slots:
0133     void updateSuggestions();
0134     void setColumnHeaders(QStringList columnHeaders);
0135     void setCompleter(CategoricalCompleter* completer);
0136     void setData(CompletionCase* data,QString prefix="");
0137     void setLineEdit(CCLineEdit* le) { bool set=_le; _le=le; if(set) le->fillKstObjects(); }
0138     void setTextEdit(CCTextEdit* te) { _te=te;}
0139 
0140 signals:
0141     void activateHint(const QString&);
0142 };
0143 
0144 }
0145 
0146 #endif // CCLINEEDIT_P_H