src/hbcore/inputfw/hbinputeditorinterface_p.cpp
changeset 6 c3690ec91ef8
parent 5 627c4a0fd0e7
child 23 e6ad4ef83b23
child 34 ed14f46c0e55
equal deleted inserted replaced
5:627c4a0fd0e7 6:c3690ec91ef8
    20 **
    20 **
    21 ** If you have questions regarding the use of this file, please contact
    21 ** If you have questions regarding the use of this file, please contact
    22 ** Nokia at developer.feedback@nokia.com.
    22 ** Nokia at developer.feedback@nokia.com.
    23 **
    23 **
    24 ****************************************************************************/
    24 ****************************************************************************/
       
    25 #include "hbinputeditorinterface.h"
       
    26 #include "hbinputeditorinterface_p.h"
       
    27 
    25 #include <QWidget>
    28 #include <QWidget>
    26 #include <QGraphicsWidget>
    29 #include <QGraphicsWidget>
    27 #include <QGraphicsProxyWidget>
    30 #include <QGraphicsProxyWidget>
    28 #include <QLineEdit>
    31 #include <QLineEdit>
    29 #include <QTextEdit>
    32 #include <QTextEdit>
    30 #include "hbinputeditorinterface_p.h"
       
    31 #include "hbinputeditorinterface.h"
       
    32 
    33 
    33 void HbEditorInterfacePrivate::lock()
    34 void HbEditorInterfacePrivate::lock()
    34 {
    35 {
    35     mMutex.lock();
    36     mMutex.lock();
    36 }
    37 }
    40     mMutex.unlock();
    41     mMutex.unlock();
    41 }
    42 }
    42 
    43 
    43 bool HbEditorInterfacePrivate::hasInterface(HbEditorInterface *toBeChecked) const
    44 bool HbEditorInterfacePrivate::hasInterface(HbEditorInterface *toBeChecked) const
    44 {
    45 {
    45     foreach (HbEditorInterface *iFace, mAttachedInterfaces) {
    46     foreach(HbEditorInterface *iFace, mAttachedInterfaces) {
    46         if (iFace == toBeChecked) {
    47         if (iFace == toBeChecked) {
    47             return true;
    48             return true;
    48         }
    49         }
    49     }
    50     }
    50 
    51 
    51     return false;
    52     return false;
    52 }
    53 }
    53 
    54 
    54 void HbEditorInterfacePrivate::setInputMethodHints(Qt::InputMethodHints hints)
    55 void HbEditorInterfacePrivate::setInputMethodHints(Qt::InputMethodHints hints)
    55 {   
    56 {
    56     if (mHostEditor) {       
    57     if (mHostEditor) {
    57          QGraphicsWidget *graphicsWidget = qobject_cast<QGraphicsWidget*>(mHostEditor); 
    58         QGraphicsWidget *graphicsWidget = qobject_cast<QGraphicsWidget *>(mHostEditor);
    58          if (graphicsWidget) {               
    59         if (graphicsWidget) {
    59              graphicsWidget->setInputMethodHints(hints);
    60             graphicsWidget->setInputMethodHints(hints);
    60          } else {
    61         } else {
    61              QWidget *widget = qobject_cast<QWidget*>(mHostEditor); 
    62             QWidget *widget = qobject_cast<QWidget *>(mHostEditor);
    62              if (widget) {            
    63             if (widget) {
    63                 widget->setInputMethodHints(hints);
    64                 widget->setInputMethodHints(hints);
    64              }                   
    65             }
    65          } 
    66         }
    66     }
    67     }
    67 }
    68 }
    68 
    69 
    69 Qt::InputMethodHints HbEditorInterfacePrivate::inputMethodHints() const
    70 Qt::InputMethodHints HbEditorInterfacePrivate::inputMethodHints() const
    70 {
    71 {
    71    if (mHostEditor) {    
    72     if (mHostEditor) {
    72        QGraphicsWidget *graphicsWidget = qobject_cast<QGraphicsWidget*>(mHostEditor); 
    73         QGraphicsWidget *graphicsWidget = qobject_cast<QGraphicsWidget *>(mHostEditor);
    73        if (graphicsWidget) {              
    74         if (graphicsWidget) {
    74            return graphicsWidget->inputMethodHints();
    75             return graphicsWidget->inputMethodHints();
    75        } else {
    76         } else {
    76             QWidget *widget = qobject_cast<QWidget*>(mHostEditor); 
    77             QWidget *widget = qobject_cast<QWidget *>(mHostEditor);
    77             if (widget) {          
    78             if (widget) {
    78                 return widget->inputMethodHints();
    79                 return widget->inputMethodHints();
    79             }     
    80             }
    80         }
    81         }
    81     }
    82     }
    82 
    83 
    83    return Qt::ImhNone;
    84     return Qt::ImhNone;
    84 }
    85 }
    85 
    86 
    86 void HbEditorInterfacePrivate::notifyCursorPositionChange(int oldPos, int newPos)
    87 void HbEditorInterfacePrivate::notifyCursorPositionChange(int oldPos, int newPos)
    87 {
    88 {
    88     emit cursorPositionChanged(oldPos, newPos);
    89     emit cursorPositionChanged(oldPos, newPos);
    95 
    96 
    96 //
    97 //
    97 // HbEditorInterfaceCache
    98 // HbEditorInterfaceCache
    98 //
    99 //
    99 
   100 
   100 HbEditorInterfacePrivateCache* HbEditorInterfacePrivateCache::instance()
   101 HbEditorInterfacePrivateCache *HbEditorInterfacePrivateCache::instance()
   101 {
   102 {
   102     static HbEditorInterfacePrivateCache myCache;
   103     static HbEditorInterfacePrivateCache myCache;
   103     return &myCache;
   104     return &myCache;
   104 }
   105 }
   105 
   106 
   112     for (int i = 0; i < mObjectCache.count(); i++) {
   113     for (int i = 0; i < mObjectCache.count(); i++) {
   113         delete mObjectCache[i];
   114         delete mObjectCache[i];
   114     }
   115     }
   115 }
   116 }
   116 
   117 
   117 HbEditorInterfacePrivate* HbEditorInterfacePrivateCache::attachEditor(QObject* editor, HbEditorInterface* interface)
   118 HbEditorInterfacePrivate *HbEditorInterfacePrivateCache::attachEditor(QObject *editor, HbEditorInterface *interface)
   118 {
   119 {
   119     if (editor) {
   120     if (editor) {
   120         for (int i = 0; i < mObjectCache.count(); i++) {
   121         for (int i = 0; i < mObjectCache.count(); i++) {
   121             if (mObjectCache[i]->mHostEditor == editor) {
   122             if (mObjectCache[i]->mHostEditor == editor) {
   122                 if (!mObjectCache[i]->hasInterface(interface)) {
   123                 if (!mObjectCache[i]->hasInterface(interface)) {
   123                     mObjectCache[i]->mAttachedInterfaces.append(interface);
   124                     mObjectCache[i]->mAttachedInterfaces.append(interface);
   124                     connect(interface, SIGNAL(destroyed(QObject*)), this, SLOT(interfaceDestroyed(QObject*)));
   125                     connect(interface, SIGNAL(destroyed(QObject *)), this, SLOT(interfaceDestroyed(QObject *)));
   125                     connect(mObjectCache[i], SIGNAL(cursorPositionChanged(int, int)), interface, SIGNAL(cursorPositionChanged(int, int)));
   126                     connect(mObjectCache[i], SIGNAL(cursorPositionChanged(int, int)), interface, SIGNAL(cursorPositionChanged(int, int)));
   126                 }
   127                 }
   127                 return mObjectCache[i];
   128                 return mObjectCache[i];
   128             }
   129             }
   129         }
   130         }
   130 
   131 
   131         HbEditorInterfacePrivate* newItem = new HbEditorInterfacePrivate();
   132         HbEditorInterfacePrivate *newItem = new HbEditorInterfacePrivate();
   132         newItem->mHostEditor = editor;
   133         newItem->mHostEditor = editor;
   133         newItem->mAttachedInterfaces.append(interface);
   134         newItem->mAttachedInterfaces.append(interface);
   134         mObjectCache.append(newItem);
   135         mObjectCache.append(newItem);
   135 
   136 
   136         connect(editor, SIGNAL(destroyed(QObject*)), this, SLOT(destroyed(QObject*)));
   137         connect(editor, SIGNAL(destroyed(QObject *)), this, SLOT(destroyed(QObject *)));
   137         connect(interface, SIGNAL(destroyed(QObject*)), this, SLOT(interfaceDestroyed(QObject*)));
   138         connect(interface, SIGNAL(destroyed(QObject *)), this, SLOT(interfaceDestroyed(QObject *)));
   138         connect(newItem, SIGNAL(cursorPositionChanged(int, int)), interface, SIGNAL(cursorPositionChanged(int, int)));
   139         connect(newItem, SIGNAL(cursorPositionChanged(int, int)), interface, SIGNAL(cursorPositionChanged(int, int)));
   139 
   140 
   140         // Check whether the connected object is a QLineEdit or QTextEdit, plain or wrapped in QGraphicsProxyWidget
   141         // Check whether the connected object is a QLineEdit or QTextEdit, plain or wrapped in QGraphicsProxyWidget
   141         // and if yes, connect its cursorPositionChanged() signal to this item's cursorPositionChanged() signal
   142         // and if yes, connect its cursorPositionChanged() signal to this item's cursorPositionChanged() signal
   142         if (editor->inherits("HbAbstractEdit")) {
   143         if (editor->inherits("HbAbstractEdit")) {
   143             connect(editor, SIGNAL(cursorPositionChanged(int, int)), newItem, SIGNAL(cursorPositionChanged(int, int)));
   144             connect(editor, SIGNAL(cursorPositionChanged(int, int)), newItem, SIGNAL(cursorPositionChanged(int, int)));
   144         } else {
   145         } else {
   145             QGraphicsProxyWidget* proxywidget = qobject_cast<QGraphicsProxyWidget*>(editor);
   146             QGraphicsProxyWidget *proxywidget = qobject_cast<QGraphicsProxyWidget *>(editor);
   146             if (proxywidget) {
   147             if (proxywidget) {
   147                 QWidget* editorwidget = proxywidget->widget();
   148                 QWidget *editorwidget = proxywidget->widget();
   148                 QLineEdit* lineedit = qobject_cast<QLineEdit*>(editorwidget);
   149                 QLineEdit *lineedit = qobject_cast<QLineEdit *>(editorwidget);
   149                 if (lineedit) {
   150                 if (lineedit) {
   150                 connect(lineedit, SIGNAL(cursorPositionChanged(int, int)), newItem, SIGNAL(cursorPositionChanged(int, int)));
   151                     connect(lineedit, SIGNAL(cursorPositionChanged(int, int)), newItem, SIGNAL(cursorPositionChanged(int, int)));
   151                 } else {
   152                 } else {
   152                     QTextEdit* textedit = qobject_cast<QTextEdit*>(editorwidget);
   153                     QTextEdit *textedit = qobject_cast<QTextEdit *>(editorwidget);
   153                     if (textedit) {
   154                     if (textedit) {
   154                     connect(textedit, SIGNAL(cursorPositionChanged()), newItem, SLOT(cursorPositionChanged()));
   155                         connect(textedit, SIGNAL(cursorPositionChanged()), newItem, SLOT(cursorPositionChanged()));
   155                     }
   156                     }
   156                 }
   157                 }
   157             } else {
   158             } else {
   158                 QLineEdit* lineedit = qobject_cast<QLineEdit*>(editor);
   159                 QLineEdit *lineedit = qobject_cast<QLineEdit *>(editor);
   159                 if (lineedit) {
   160                 if (lineedit) {
   160                 connect(lineedit, SIGNAL(cursorPositionChanged(int, int)), newItem, SIGNAL(cursorPositionChanged(int, int)));
   161                     connect(lineedit, SIGNAL(cursorPositionChanged(int, int)), newItem, SIGNAL(cursorPositionChanged(int, int)));
   161                 } else {
   162                 } else {
   162                     QTextEdit* textedit = qobject_cast<QTextEdit*>(editor);
   163                     QTextEdit *textedit = qobject_cast<QTextEdit *>(editor);
   163                     if (textedit) {
   164                     if (textedit) {
   164                     connect(textedit, SIGNAL(cursorPositionChanged()), newItem, SLOT(cursorPositionChanged()));
   165                         connect(textedit, SIGNAL(cursorPositionChanged()), newItem, SLOT(cursorPositionChanged()));
   165                     }
   166                     }
   166                 }
   167                 }
   167             }
   168             }
   168         }
   169         }
   169 
   170 
   171     }
   172     }
   172 
   173 
   173     return 0;
   174     return 0;
   174 }
   175 }
   175 
   176 
   176 void HbEditorInterfacePrivateCache::destroyed(QObject* object)
   177 void HbEditorInterfacePrivateCache::destroyed(QObject *object)
   177 {
   178 {
   178     for (int i = 0; i < mObjectCache.count(); i++) {
   179     for (int i = 0; i < mObjectCache.count(); i++) {
   179         if (mObjectCache[i]->mHostEditor == object) {
   180         if (mObjectCache[i]->mHostEditor == object) {
   180             delete mObjectCache[i];
   181             delete mObjectCache[i];
   181             mObjectCache.remove(i);
   182             mObjectCache.remove(i);
   182             break;
   183             break;
   183         }
   184         }
   184     }
   185     }
   185 }
   186 }
   186 
   187 
   187 void HbEditorInterfacePrivateCache::interfaceDestroyed(QObject* object)
   188 void HbEditorInterfacePrivateCache::interfaceDestroyed(QObject *object)
   188 {
   189 {
   189     for (int i = 0; i < mObjectCache.count(); i++) {
   190     for (int i = 0; i < mObjectCache.count(); i++) {
   190         for (int j = 0; j < mObjectCache[i]->mAttachedInterfaces.count(); j++) {
   191         for (int j = 0; j < mObjectCache[i]->mAttachedInterfaces.count(); j++) {
   191             if (mObjectCache[i]->mAttachedInterfaces[j] == object) {
   192             if (mObjectCache[i]->mAttachedInterfaces[j] == object) {
   192                 mObjectCache[i]->mAttachedInterfaces.removeAt(j);
   193                 mObjectCache[i]->mAttachedInterfaces.removeAt(j);
   194             }
   195             }
   195         }
   196         }
   196     }
   197     }
   197 }
   198 }
   198 
   199 
   199 void HbEditorInterfacePrivateCache::actionDestroyed(QObject* object)
   200 void HbEditorInterfacePrivateCache::actionDestroyed(QObject *object)
   200 {
   201 {
   201     foreach(HbEditorInterfacePrivate *editorInterfacePrivate, mObjectCache) {
   202     foreach(HbEditorInterfacePrivate *editorInterfacePrivate, mObjectCache) {
   202         HbAction *action = static_cast<HbAction *>(object);
   203         HbAction *action = static_cast<HbAction *>(object);
   203         if (editorInterfacePrivate->mActions.contains(action)) {
   204         if (editorInterfacePrivate->mActions.contains(action)) {
   204             editorInterfacePrivate->mActions.removeAll(action);
   205             editorInterfacePrivate->mActions.removeAll(action);
   207             }
   208             }
   208         }
   209         }
   209     }
   210     }
   210 }
   211 }
   211 
   212 
   212 void HbEditorInterfacePrivateCache::notifyValueChanged(QObject* editor)
   213 void HbEditorInterfacePrivateCache::notifyValueChanged(QObject *editor)
   213 {
   214 {
   214     for (int i = 0; i < mObjectCache.count(); i++) {
   215     for (int i = 0; i < mObjectCache.count(); i++) {
   215        if (mObjectCache[i]->mHostEditor == editor) {
   216         if (mObjectCache[i]->mHostEditor == editor) {
   216             for (int j = 0; j < mObjectCache[i]->mAttachedInterfaces.count(); j++) {
   217             for (int j = 0; j < mObjectCache[i]->mAttachedInterfaces.count(); j++) {
   217                 mObjectCache[i]->mAttachedInterfaces[j]->backendModified();
   218                 mObjectCache[i]->mAttachedInterfaces[j]->backendModified();
   218             }
   219             }
   219             break;
   220             break;
   220         }
   221         }
   221     }
   222     }
   222 }
   223 }
   223 
   224 
   224 bool HbEditorInterfacePrivateCache::isConnected(QObject* object)
   225 bool HbEditorInterfacePrivateCache::isConnected(QObject *object)
   225 {
   226 {
   226     for (int i = 0; i < mObjectCache.count(); i++) {
   227     for (int i = 0; i < mObjectCache.count(); i++) {
   227         if (mObjectCache[i]->mHostEditor == object) {
   228         if (mObjectCache[i]->mHostEditor == object) {
   228             return true;
   229             return true;
   229         }
   230         }