[Kstars-devel] KDE/kdeedu/kstars/kstars/tools

Alexey Khudyakov alexey.skladnoy at gmail.com
Tue Jun 16 20:46:10 CEST 2009


SVN commit 982750 by khudyakov:

Speeding up calculator startup.

Calculator takes annoyingly long to start. >1s on my box. Most of this
time is spend in widget construction. One solution is to construct widget only
when it's requested. This changeset introduce such behavior. 

CCMAIL: kstars-devel at kde.org



 M  +22 -14    astrocalc.cpp  
 M  +26 -4     astrocalc.h  


--- trunk/KDE/kdeedu/kstars/kstars/tools/astrocalc.cpp #982749:982750
@@ -110,7 +110,7 @@
              "</LI></UL>"
              "</QT>");
 
-    split = new QSplitter ( this );
+    QSplitter* split = new QSplitter ( this );
     setMainWidget(split);
     setCaption( i18n("Calculator") );
     setButtons( KDialog::Close );
@@ -128,6 +128,10 @@
 
     splashScreen = new KTextEdit( message, acStack );
     splashScreen->setReadOnly( true );
+    //FIXME: Minimum size is set to resize calculator to correct size
+    //when no modules is loaded. This is simply biggest size of
+    //calculator modules. I think it should be set in more cleverly.
+    splashScreen->setMinimumSize(640, 550);
     acStack->addWidget( splashScreen );
 
 
@@ -166,12 +170,12 @@
     addTreeItem<ConjunctionsTool>(solarItem, i18n("Conjunctions"));
     
     acStack->setCurrentWidget( splashScreen );
-    connect(navigationPanel, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this,
-            SLOT(slotItemSelection(QTreeWidgetItem *)));
+    connect(navigationPanel, SIGNAL(itemClicked(QTreeWidgetItem *, int)),
+            this, SLOT(slotItemSelection(QTreeWidgetItem *)));
 }
 
 template<typename T>
-T* AstroCalc::addToStack()
+QWidget* AstroCalc::addToStack()
 {
     T* t = new T( acStack );
     acStack->addWidget(t);
@@ -182,7 +186,8 @@
 QTreeWidgetItem* AstroCalc::addTreeItem(QTreeWidgetItem* parent, QString title)
 {
     QTreeWidgetItem* item = new QTreeWidgetItem(parent, QStringList(title));
-    dispatchTable.insert(item, addToStack<T>());
+    dispatchTable.insert(item,
+                         WidgetThunk(this, &AstroCalc::addToStack<T>));
     return item;
 }
 
@@ -193,18 +198,12 @@
     return item;
 }
 
-AstroCalc::~AstroCalc()
-{
-}
+AstroCalc::~AstroCalc() {}
 
 void AstroCalc::slotItemSelection(QTreeWidgetItem *item)
 {
     if ( item == 0)
         return;
-
-    //DEBUG
-    kDebug() << "Item clicked: " << item->text(0);
-
     // Lookup in HTML table
     QMap<QTreeWidgetItem*, QString>::iterator iterHTML = htmlTable.find(item);
     if( iterHTML != htmlTable.end() ) {
@@ -213,9 +212,9 @@
         return;
     }
     // Lookup in frames table
-    QMap<QTreeWidgetItem*, QWidget*>::iterator iter = dispatchTable.find(item);
+    QMap<QTreeWidgetItem*, WidgetThunk>::iterator iter = dispatchTable.find(item);
     if( iter != dispatchTable.end() ) {
-        acStack->setCurrentWidget( *iter );
+        acStack->setCurrentWidget( iter->eval() );
     }
 }
 
@@ -224,4 +223,13 @@
     return QSize(640,430);
 }
 
+QWidget* AstroCalc::WidgetThunk::eval()
+{
+    if( widget == 0 ) {
+        // This is pointer to member function call. 
+        widget = (calc->*func)();
+    }
+    return widget;
+}
+
 #include "astrocalc.moc"
--- trunk/KDE/kdeedu/kstars/kstars/tools/astrocalc.h #982749:982750
@@ -36,7 +36,6 @@
 
 class AstroCalc : public KDialog
 {
-
     Q_OBJECT
 public:
     AstroCalc(QWidget *parent = 0);
@@ -51,10 +50,34 @@
     void slotItemSelection(QTreeWidgetItem *it);
 
 private:
+    /** Pointer to function which return QWidget* 
+     */
+    typedef QWidget* (AstroCalc::*WidgetConstructor)();
+    /** Data structure used for lazy widget construction. This class
+     *  construct widget when it requested. 
+     */
+    class WidgetThunk
+    {
+    public:
+        /** Create thunk
+         *  @param acalc  pointer to class.
+         *  @param f      function which construct widget.
+         */
+        WidgetThunk(AstroCalc* acalc, WidgetConstructor f) :
+            widget(0), calc(acalc), func(f) {}
+        /** Request widget.
+         *  @return newly created widget or cached value. */
+        QWidget* eval();
+    private:
+        QWidget* widget;        // Cached value
+        AstroCalc* calc;        // Pointer to calculator
+        WidgetConstructor func; // Function to call to construct widget. 
+    };
+    
     /** Create widget of type T and put it to widget stack. Widget must
      *  have construtor of type T(QWidget*). Returns constructed widget. */
     template<typename T>
-    inline T* addToStack();
+    inline QWidget* addToStack();
     
     /** Add top level item to navigation panel. At the same time adds item to htmlTable 
         @param title name of item
@@ -75,8 +98,7 @@
     /** Lookup table for help texts. Maps navpanel item to help text. */
     QMap<QTreeWidgetItem*, QString>  htmlTable;
     /** Lookup table for widgets. Maps navpanel item to widget to be displayed. */
-    QMap<QTreeWidgetItem*, QWidget*> dispatchTable;
-    QSplitter *split;
+    QMap<QTreeWidgetItem*, WidgetThunk> dispatchTable;
     QTreeWidget *navigationPanel;
     QStackedWidget *acStack;
     KTextEdit *splashScreen;


More information about the Kstars-devel mailing list