kexi

utils.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
00003    Copyright (C) 2007 Jaroslaw Staniek <js@iidea.pl>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include <qcursor.h>
00022 #include <qobjectlist.h>
00023 #include <qtabwidget.h>
00024 #include <qtabbar.h>
00025 
00026 #include <kdebug.h>
00027 #include <kexiutils/utils.h>
00028 
00029 #include "form.h"
00030 #include "objecttree.h"
00031 #include "utils.h"
00032 
00033 using namespace KFormDesigner;
00034 
00035 void
00036 KFormDesigner::removeChildrenFromList(WidgetList &list)
00037 {
00038     for(WidgetListIterator it(list); it.current() != 0; ++it)  {
00039         QWidget *w = it.current();
00040 
00041         // If any widget in the list is a child of this widget, we remove it from the list
00042         for(WidgetListIterator it2(list); it2.current() != 0; ++it2) {
00043             QWidget *widg = it2.current();
00044             if((w != widg) && (w->child(widg->name())))
00045             {
00046                 kdDebug() << "Removing the widget " << widg->name() << "which is a child of " << w->name() << endl;
00047                 list.remove(widg);
00048             }
00049         }
00050     }
00051 }
00052 
00053 void
00054 KFormDesigner::installRecursiveEventFilter(QObject *object, QObject *container)
00055 {
00056     if(!object || !container|| !object->isWidgetType())
00057         return;
00058 
00059     kdDebug() << "Installing event filter on widget: " << object->name() << " directed to " << container->name() << endl;
00060     object->installEventFilter(container);
00061     if(((QWidget*)object)->ownCursor())
00062         ((QWidget*)object)->setCursor(QCursor(Qt::ArrowCursor));
00063 
00064     if(!object->children())
00065         return;
00066 
00067     QObjectList list = *(object->children());
00068     for(QObject *obj = list.first(); obj; obj = list.next())
00069         installRecursiveEventFilter(obj, container);
00070 }
00071 
00072 void
00073 KFormDesigner::removeRecursiveEventFilter(QObject *object, QObject *container)
00074 {
00075     object->removeEventFilter(container);
00076     if(!object->isWidgetType())
00077         return;
00078     if(!object->children())
00079         return;
00080 
00081     QObjectList list = *(object->children());
00082     for(QObject *obj = list.first(); obj; obj = list.next())
00083         removeRecursiveEventFilter(obj, container);
00084 }
00085 
00086 void
00087 KFormDesigner::setRecursiveCursor(QWidget *w, Form *form)
00088 {
00089     ObjectTreeItem *tree = form->objectTree()->lookup(w->name());
00090     if(tree && ((tree->modifiedProperties()->contains("cursor")) || !tree->children()->isEmpty())
00091         && !w->inherits("QLineEdit") && !w->inherits("QTextEdit")
00092         ) //fix weird behaviour
00093         return; // if the user has set a cursor for this widget or this is a container, don't change it
00094 
00095     if(w->ownCursor())
00096         w->setCursor(Qt::ArrowCursor);
00097 
00098     QObjectList *l = w->queryList( "QWidget" );
00099     for(QObject *o = l->first(); o; o = l->next())
00100         ((QWidget*)o)->setCursor(Qt::ArrowCursor);
00101     delete l;
00102 }
00103 
00104 QSize
00105 KFormDesigner::getSizeFromChildren(QWidget *w, const char *inheritClass)
00106 {
00107     int tmpw = 0, tmph = 0;
00108     QObjectList *list = w->queryList(inheritClass, 0, false, false);
00109     for(QObject *o = list->first(); o; o = list->next()) {
00110         QRect  r = ((QWidget*)o)->geometry();
00111         tmpw = QMAX(tmpw, r.right());
00112         tmph = QMAX(tmph, r.bottom());
00113     }
00114 
00115     delete list;
00116     return QSize(tmpw, tmph) + QSize(10, 10);
00117 }
00118 
00119 // -----------------
00120 
00121 HorWidgetList::HorWidgetList(QWidget *topLevelWidget)
00122     : WidgetList()
00123     , m_topLevelWidget(topLevelWidget)
00124 {
00125 }
00126 
00127 HorWidgetList::~HorWidgetList()
00128 {
00129 }
00130 
00131 int HorWidgetList::compareItems(QPtrCollection::Item item1, QPtrCollection::Item item2)
00132 {
00133     QWidget *w1 = static_cast<QWidget*>(item1);
00134     QWidget *w2 = static_cast<QWidget*>(item2);
00135     return w1->mapTo(m_topLevelWidget, QPoint(0,0)).x() - w2->mapTo(m_topLevelWidget, QPoint(0,0)).x();
00136 }
00137 
00138 // -----------------
00139 
00140 VerWidgetList::VerWidgetList(QWidget *topLevelWidget)
00141     : WidgetList()
00142     , m_topLevelWidget(topLevelWidget)
00143 {
00144 }
00145 
00146 VerWidgetList::~VerWidgetList()
00147 {
00148 }
00149 
00150 int VerWidgetList::compareItems(QPtrCollection::Item item1, QPtrCollection::Item item2)
00151 {
00152     QWidget *w1 = static_cast<QWidget*>(item1);
00153     QWidget *w2 = static_cast<QWidget*>(item2);
00154 
00155     int y1, y2;
00156     QObject *page1 = 0;
00157     TabWidget *tw1 = KFormDesigner::findParent<KFormDesigner::TabWidget>(w1, "KFormDesigner::TabWidget", page1);
00158     if (tw1) // special case
00159         y1 = w1->mapTo(m_topLevelWidget, QPoint(0,0)).y() + tw1->tabBarHeight() -2 -2;
00160     else
00161         y1 = w1->mapTo(m_topLevelWidget, QPoint(0,0)).y();
00162 
00163     QObject *page2 = 0;
00164     TabWidget *tw2 = KFormDesigner::findParent<KFormDesigner::TabWidget>(w2, "KFormDesigner::TabWidget", page2);
00165     if (tw1 && tw2 && tw1 == tw2 && page1 != page2) {
00166         // this sorts widgets by tabs there're put in
00167         return tw1->indexOf(static_cast<QWidget*>(page1)) - tw2->indexOf(static_cast<QWidget*>(page2));
00168     }
00169 
00170     if (tw2) // special case
00171         y2 = w2->mapTo(m_topLevelWidget, QPoint(0,0)).y() + tw2->tabBarHeight() -2 -2;
00172     else
00173         y2 = w2->mapTo(m_topLevelWidget, QPoint(0,0)).y();
00174     
00175     kdDebug() << w1->name() << ": " << y1 << " " 
00176         << " | " << w2->name() << ": " << y2 << endl;
00177 
00178 
00179     //kdDebug() << w1->name() << ": " << w1->mapTo(m_topLevelWidget, QPoint(0,0)) << " " << w1->y()
00180         //<< " | " << w2->name() << ":" /*<< w2->mapFrom(m_topLevelWidget, QPoint(0,w2->y()))*/ << " " << w2->y() << endl;
00181     return y1 - y2;
00182 }
00183 
00184 #include "utils.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys