[Kst] branches/work/kst/portto4/kst/src

Joshua Netterfield drmrshdw at gmail.com
Thu Aug 11 21:02:44 UTC 2011


SVN commit 1246519 by joshuanetterfield:

Added a heuristic for determining size limited name lengths in the categorical completion widget found in the equation and label editor dialogs.
Let me know if a better heuristic would be something worth investing in.

Also added a function in NamedObject which returns a name size-limited by font and width.



 M  +8 -4      libkst/namedobject.cpp  
 M  +1 -0      libkst/namedobject.h  
 M  +22 -8     widgets/cclineedit.cpp  
 M  +4 -0      widgets/cclineedit.h  
 M  +6 -1      widgets/cclineedit_p.h  


--- branches/work/kst/portto4/kst/src/libkst/namedobject.cpp #1246518:1246519
@@ -70,24 +70,28 @@
 
 }
 
-QString NamedObject::sizeLimitedName(const QWidget *widget ) const {
+QString NamedObject::sizeLimitedName(const QFont& font, const int& width) const {
+    QFontMetrics fontMetrics=QFontMetrics(font);
   // initial guess
-  int combo_chars = widget->width() / widget->fontMetrics().averageCharWidth() - 2;
+    int combo_chars = width / fontMetrics.averageCharWidth() - 2;
   int nameLength = Name().length();
 
   QString name = lengthLimitedName(combo_chars);
   while ((combo_chars <= nameLength+1) &&
-         (widget->fontMetrics().width(name) < widget->width() - widget->fontMetrics().maxWidth())) {
+           (fontMetrics.width(name) < width - fontMetrics.maxWidth())) {
     combo_chars++;
     name = lengthLimitedName(combo_chars);
   }
   while ((combo_chars>0) &&
-         (widget->fontMetrics().width(name) > widget->width()  - widget->fontMetrics().maxWidth())) {
+           (fontMetrics.width(name) > width  - fontMetrics.maxWidth())) {
     combo_chars--;
     name = lengthLimitedName(combo_chars);
   }
   return name;
+}
 
+QString NamedObject::sizeLimitedName(const QWidget *widget ) const {
+  return sizeLimitedName(widget->font(),widget->width());
 }
 
 QString NamedObject::CleanedName() const {
--- branches/work/kst/portto4/kst/src/libkst/namedobject.h #1246518:1246519
@@ -89,6 +89,7 @@
     QString descriptiveName() const; // eg GYRO1: automatic or manual
     QString shortName() const; // eg V1: always automatically generated
     QString lengthLimitedName(int length = 20) const; // Name, but with descriptiveName truncated
+    QString sizeLimitedName(const QFont&font,const int&width) const; // Name, shrunk to fit in width with font
     QString sizeLimitedName(const QWidget *widget) const; // Name, shrunk to fit in widget
     virtual QString descriptionTip() const = 0; // description for tooltips
     void setDescriptiveName(QString new_name); // auto if new_name.isEmpty()
--- branches/work/kst/portto4/kst/src/widgets/cclineedit.cpp #1246518:1246519
@@ -553,6 +553,8 @@
     CCTextEdit::init(*_allData);
 }
 
+#define SIZE_LIMITED_NAME sizeLimitedName( (_cc&&_cc->_tableView)?_cc->_tableView->font():QFont(), \
+    (_cc&&_cc->_tableView)? ((_cc->_tableView->width()/2-50)): 900000000000)
 
 void SVCCLineEdit::fillKstObjects()
 {
@@ -563,6 +565,7 @@
     if(_svData) {
         delete _svData;
     }
+    CCLineEdit::init(*_extraData);
     _svData = new QList<CompletionCase>;
     _svData->push_back(CompletionCase(""));
     _svData->back().push_back(Category("Scalars"));
@@ -581,8 +584,8 @@
         ScalarPtr scalar = (*scalarIt);
 
         scalar->readLock();
-        _svData->back()[0].push_back(scalar->lengthLimitedName(40)+"]");
-        _svData->front()[0].push_back("["+scalar->lengthLimitedName(40)+"]");
+        _svData->back()[0].push_back(scalar->SIZE_LIMITED_NAME+"]");
+        _svData->front()[0].push_back("["+scalar->SIZE_LIMITED_NAME+"]");
         scalar->unlock();
     }
 
@@ -591,8 +594,8 @@
         VectorPtr vector = (*vectorIt);
 
         vector->readLock();
-        _svData->back()[1].push_back(vector->lengthLimitedName(40)+"]");
-        _svData->front()[1].push_back("["+vector->lengthLimitedName(40)+"]");
+        _svData->back()[1].push_back(vector->SIZE_LIMITED_NAME+"]");
+        _svData->front()[1].push_back("["+vector->SIZE_LIMITED_NAME+"]");
         vector->unlock();
     }
 
@@ -608,6 +611,8 @@
     if(_svData) {
         delete _svData;
     }
+    if(!_extraData) _extraData=new QList<CompletionCase>;
+    CCTextEdit::init(*_extraData);
     _svData = new QList<CompletionCase>;
     _svData->push_back(CompletionCase("["));
     _svData->back().push_back(Category("Scalars"));
@@ -620,7 +625,7 @@
             ScalarPtr scalar = (*scalarIt);
 
             scalar->readLock();
-            _svData->back()[0].push_back(scalar->lengthLimitedName(40)+"]");
+            _svData->back()[0].push_back(scalar->SIZE_LIMITED_NAME+"]");
             scalar->unlock();
         }
 
@@ -631,7 +636,7 @@
         StringPtr string = (*stringIt);
 
         string->readLock();
-        _svData->back()[1].push_back(string->lengthLimitedName(40)+"]");
+        _svData->back()[1].push_back(string->SIZE_LIMITED_NAME+"]");
         string->unlock();
     }
 
@@ -871,9 +876,11 @@
 QVariant CCTableModel::data(const QModelIndex &index, int role) const
 {
     if(role==Qt::SizeHintRole) {    //evil problems have evil solutions -_-
+        emit checkSize();
         if(index.column()>=columnCount(QModelIndex())) {
             return QVariant();
         }
+
         if(s_minSizeCache[index.column()]!=QSize(-1,-1)) {
             return s_minSizeCache[index.column()];
         }
@@ -965,7 +972,8 @@
             horizontalHeader()->showSection(i);
         }
     }
-    setModel(new CCTableModel(items));
+    CCTableModel* tmodel=new CCTableModel(items);
+    setModel(tmodel);
     int minOK=-1;
     int maxOK=-1;
     for(int i=0;i<_data->size();i++) {
@@ -979,6 +987,7 @@
     }
 
     resizeColumnsToContents();
+    horizontalHeader()->setStretchLastSection(1);
     completer->complete();
 }
 
@@ -1031,10 +1040,15 @@
 {
     //    resizeColumnsToContents();
     int bestWidth=qMax(width(),(int)((horizontalHeader()->length()+verticalScrollBar()->width())));
-    horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
+    horizontalHeader()->setResizeMode(QHeaderView::Interactive);
     horizontalHeader()->setStretchLastSection(1);
     setMinimumWidth(bestWidth);
     setMaximumWidth(bestWidth);
 }
 
+void CCTableView::resizeEvent(QResizeEvent*ev)
+{
+    QTableView::resizeEvent(ev);
 }
+
+}
--- branches/work/kst/portto4/kst/src/widgets/cclineedit.h #1246518:1246519
@@ -95,6 +95,7 @@
     virtual void init(QList<CompletionCase> data);
 
 public slots:
+    virtual void fillKstObjects() {}
     void insert(const QString &i,bool stringIsCompletion=true);
 protected slots:
     void divide(QString x="\0");
@@ -122,6 +123,7 @@
 
     void setObjectStore(ObjectStore *store);
     virtual void init(QList<CompletionCase> data=QList<CompletionCase>());
+public Q_SLOTS:
     void fillKstObjects();
 protected:
     void contextMenuEvent(QContextMenuEvent *);
@@ -155,6 +157,7 @@
 
 public slots:
     void insert(const QString &i,bool stringIsCompletion=true);
+    virtual void fillKstObjects() {}
 protected slots:
     void divide(QString x="\0");
 
@@ -179,6 +182,7 @@
 
     void setObjectStore(ObjectStore *store);
     virtual void init(QList<CompletionCase> data=QList<CompletionCase>());
+public slots:
     void fillKstObjects();
 protected:
     void contextMenuEvent(QContextMenuEvent *);
--- branches/work/kst/portto4/kst/src/widgets/cclineedit_p.h #1246518:1246519
@@ -33,6 +33,8 @@
     friend class CCCommonEdit;
     friend class CCLineEdit;
     friend class CCTextEdit;
+    friend class SVCCLineEdit;
+    friend class SVCCTextEdit;
     /**
    * Creates an autocompleter for child.
    *
@@ -73,6 +75,8 @@
     QVariant data(const QModelIndex &index, int role) const;
     Qt::ItemFlags flags(const QModelIndex &index) const;
     QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
+signals:
+    void checkSize() const;
 };
 
 class CCTableView : public QTableView {
@@ -97,6 +101,7 @@
     void keyPressEvent(QKeyEvent *event);
     void mousePressEvent(QMouseEvent *event);
     void showEvent(QShowEvent *);
+    void resizeEvent(QResizeEvent *event);
     int widgetCursorPosition() {
         if(_le) {
             return _le->cursorPosition();
@@ -123,7 +128,7 @@
     void setColumnHeaders(QStringList columnHeaders);
     void setCompleter(CategoricalCompleter* completer);
     void setData(CompletionCase* data,QString prefix="");
-    void setLineEdit(CCLineEdit* le) { _le=le;}
+    void setLineEdit(CCLineEdit* le) { bool set=_le; _le=le; if(set) le->fillKstObjects(); }
     void setTextEdit(CCTextEdit* te) { _te=te;}
 
 signals:


More information about the Kst mailing list