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 #ifndef CCLINEEDIT_H
0014 #define CCLINEEDIT_H
0015 
0016 #include <QLineEdit>
0017 #include <QTextEdit>
0018 #include <QStringList>
0019 #include "kstwidgets_export.h"
0020 
0021 namespace Kst {
0022 
0023 /**
0024  * A category is a list of possible completions under a certain title.
0025  * For example, a Category could be a list of Vector names.
0026  */
0027 class Category : public QStringList {
0028     QString _title;
0029 public:
0030     Category(QString title) : QStringList(), _title(title) { }
0031     Category(QString title, QString i) : QStringList(i), _title(title) { }
0032     Category(QString title, QStringList l) : QStringList(l), _title(title) { }
0033     const QString& title() const { return _title; }
0034 };
0035 
0036 /**
0037  * A completion case is a prefix for Categories.
0038  */
0039 class CompletionCase : public QList<Category> {
0040     QString _prefix;
0041 public:
0042     CompletionCase(QString prefix) : QList<Category>(), _prefix(prefix) { }
0043     CompletionCase(QString prefix,QList<Category> l) : QList<Category>(l), _prefix(prefix) { }
0044     const QString& prefix() const { return _prefix; }
0045 };
0046 
0047 bool operator<(const CompletionCase&a,const CompletionCase&b);
0048 
0049 class CategoricalCompleter;
0050 class ObjectStore;
0051 
0052 class CCCommonEdit {
0053 protected:
0054     ObjectStore *_store;
0055     static QList<CCCommonEdit*> _u;
0056     virtual QString Text()=0;
0057     virtual void SetText(QString text)=0;
0058     virtual int CursorPosition()=0;
0059     virtual void SetCursorPosition(int)=0;
0060     virtual void NewPrefix(QString)=0;
0061     virtual void SetCompleter(QCompleter*) {}
0062     virtual void ChangeCurrentPrefix(QString)=0;
0063     void Divide(QString x="\0");
0064     virtual void fillKstObjects() {}
0065     void NewVector();
0066     void NewScalar();
0067     void NewString();
0068     void EditItem();
0069     void Insert(const QString&i,bool stringIsCompletion=true);
0070     CCCommonEdit() : _store(0) {_u.push_back(this); }
0071     ~CCCommonEdit() { _u.removeOne(this); }
0072 };
0073 
0074 /**
0075  * This is a LineEdit with autocompletion. The completion items are divided into cases, categories and items.
0076  * @sa CompletionCase, Category
0077  */
0078 class KSTWIDGETS_EXPORT CCLineEdit : public QLineEdit, public CCCommonEdit {
0079     Q_OBJECT
0080 protected:
0081     CategoricalCompleter* _cc;
0082     virtual QString Text(){return text();}
0083     virtual void SetText(QString text){setText(text);}
0084     virtual int CursorPosition(){return cursorPosition();}
0085     virtual void SetCursorPosition(int x){setCursorPosition(x);}
0086     virtual void NewPrefix(QString x){emit currentPrefixChanged(x);}
0087     virtual void SetCompleter(QCompleter*c) {setCompleter(c);}
0088     virtual void ChangeCurrentPrefix(QString x);
0089 
0090 public:
0091     friend class CCTableView;
0092     explicit CCLineEdit(QWidget*p=0);
0093     explicit CCLineEdit(const QString&s,QWidget*p=0);
0094     ~CCLineEdit();
0095     virtual void init(QList<CompletionCase> data);
0096 
0097 public slots:
0098     virtual void fillKstObjects() {}
0099     void insert(const QString &i,bool stringIsCompletion=true);
0100 protected slots:
0101     void divide(QString x="\0");
0102 
0103 protected:
0104     void keyPressEvent(QKeyEvent *);
0105     void mousePressEvent(QMouseEvent *);
0106 signals:
0107     void currentPrefixChanged(QString);
0108 };
0109 
0110 /**
0111  * This is a special version of CCLineEdit which is used to display scalars and vectors.
0112  * You can still send your own QList<CompletionCase>* in init() which will be added to the list of scalars and vectors.
0113  * @sa CCLineEdit
0114  */
0115 class KSTWIDGETS_EXPORT SVCCLineEdit : public CCLineEdit
0116 {
0117     Q_OBJECT
0118     QList<CompletionCase> *_extraData, *_svData, *_allData;
0119 public:
0120     explicit SVCCLineEdit(QWidget*p=0);
0121     explicit SVCCLineEdit(const QString&s,QWidget*p=0);
0122     ~SVCCLineEdit();
0123 
0124     void setObjectStore(ObjectStore *store);
0125     virtual void init(QList<CompletionCase> data=QList<CompletionCase>());
0126 public Q_SLOTS:
0127     void fillKstObjects();
0128 protected:
0129     void contextMenuEvent(QContextMenuEvent *);
0130 public slots:
0131     inline void newVector(){NewVector();}
0132     inline void newScalar(){NewScalar();}
0133     inline void editItem(){EditItem();}
0134 };
0135 
0136 /**
0137  * This is a TextEdit with audocompletion.
0138  * @sa CCLineEdit
0139  */
0140 class KSTWIDGETS_EXPORT CCTextEdit : public QTextEdit, public CCCommonEdit {
0141     Q_OBJECT
0142 protected:
0143     CategoricalCompleter* _cc;
0144     virtual QString Text(){return toPlainText();}
0145     virtual void SetText(QString text){bool dirty=document()->isModified();setPlainText(text);document()->setModified(dirty);}
0146     virtual int CursorPosition(){return textCursor().position();}
0147     virtual void SetCursorPosition(int x){QTextCursor tc=textCursor();tc.setPosition(x);setTextCursor(tc);}
0148     virtual void NewPrefix(QString x){emit currentPrefixChanged(x);}
0149     virtual void SetCompleter(QCompleter*) {}
0150     virtual void ChangeCurrentPrefix(QString x);
0151 public:
0152     friend class CCTableView;
0153     explicit CCTextEdit(QWidget*p=0);
0154     explicit CCTextEdit(const QString&s,QWidget*p=0);
0155     ~CCTextEdit();
0156     virtual void init(QList<CompletionCase> data);
0157 
0158 public slots:
0159     void insert(const QString &i,bool stringIsCompletion=true);
0160     virtual void fillKstObjects() {}
0161 protected slots:
0162     void divide(QString x="\0");
0163 
0164 protected:
0165     void keyPressEvent(QKeyEvent *e);
0166 signals:
0167     void currentPrefixChanged(QString);
0168 };
0169 
0170 /**
0171  * This is a special version of CCTextEdit which is used to display scalars and vectors.
0172  * @sa SVCCLineEdit
0173  */
0174 class KSTWIDGETS_EXPORT SVCCTextEdit : public CCTextEdit
0175 {
0176     Q_OBJECT
0177     QList<CompletionCase> *_extraData, *_svData, *_allData;
0178 public:
0179     explicit SVCCTextEdit(QWidget*p=0);
0180     explicit SVCCTextEdit(const QString&s,QWidget*p=0);
0181     ~SVCCTextEdit();
0182 
0183     void setObjectStore(ObjectStore *store);
0184     virtual void init(QList<CompletionCase> data=QList<CompletionCase>());
0185 public slots:
0186     void fillKstObjects();
0187 protected:
0188     void contextMenuEvent(QContextMenuEvent *);
0189 public slots:
0190     inline void newVector(){NewVector();}
0191     inline void newScalar(){NewScalar();}
0192     inline void newString(){NewString();}
0193     inline void editItem(){EditItem();}
0194 };
0195 
0196 }
0197 
0198 #endif // ACLINEEDIT_H