]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt2/QBrowseBox.C
Some string(widget->text()) fixes. Weirdness
[lyx.git] / src / frontends / qt2 / QBrowseBox.C
index 86cdd25c73e14914b853a855fb2f9ca17b3d6914..a76dd75069821a76fc32497931e9b0927391475e 100644 (file)
@@ -2,13 +2,13 @@
  * \file QBrowseBox.C
  *
  * Original file taken from klyx 0.10 sources:
- * $Id: QBrowseBox.C,v 1.5 2002/10/17 13:31:04 leeming Exp $
  *
- * \author Kalle Dalheimer ?
+ * \author Kalle Dalheimer
  *
  * Full author contact details are available in file CREDITS
  */
 
+#include <config.h>
 
 #include <qstring.h>
 #include <qpixmap.h>
 #include <qstyle.h>
 #include <qimage.h>
 
-#include <stdio.h>
-#include <math.h>
-
 #include "QBrowseBox.h"
 
+#include <cstdio>
+#include <cmath>
+
 
-QBrowseBox::QBrowseBox(int rows, int cols)
-       : QGridView()
+QBrowseBox::QBrowseBox(int rows, int cols, QWidget* parent, const char* name, WFlags f)
+       : QGridView(parent,name,f)
 {
        setNumRows(rows);
        setNumCols(cols);
@@ -36,17 +36,19 @@ QBrowseBox::QBrowseBox(int rows, int cols)
        texts_   = new QString[rows * cols];
        pixmaps_ = new QPixmap[rows * cols];
 
-       activecell_.setX(0);
-       activecell_.setY(0);
-       updateCell(0, 0);
-       setMouseTracking(true);
+       activecell_.setX(-1);
+       activecell_.setY(-1);
 
        if (style().inherits("QWindowsStyle"))
                setFrameStyle(QFrame::WinPanel | QFrame::Raised);
        else
                setFrameStyle(QFrame::Panel | QFrame::Raised);
 
-       setFocusPolicy(QWidget::StrongFocus);
+       viewport()->setFocusPolicy(QWidget::StrongFocus);
+       // setMouseTracking must be called after setFocusPolicy
+       viewport()->setMouseTracking(true);
+       inloop=false;
+       
 }
 
 
@@ -72,9 +74,9 @@ void QBrowseBox::insertItem(QString const & text, int row, int col)
 }
 
 
-void QBrowseBox::insertItem(char const * text, int x, int y)
+void QBrowseBox::insertItem(char const * text, int row, int col)
 {
-       insertItem(QString(text), x, y);
+       insertItem(QString(text), row, col);
 }
 
 
@@ -123,7 +125,6 @@ QString QBrowseBox::text(int row, int col)
        return texts_[coordsToIndex(row, col)];
 }
 
-
 QPixmap QBrowseBox::pixmap(int row, int col)
 {
        if (col < 0 || col >= numCols() || row < 0 || row >= numRows())
@@ -131,7 +132,49 @@ QPixmap QBrowseBox::pixmap(int row, int col)
        return pixmaps_[coordsToIndex(row, col)];
 }
 
+int QBrowseBox::exec(const QPoint& pos)
+{
+       return exec(pos.x(),pos.y());
+}
+
+int QBrowseBox::exec(const QWidget* trigger)
+{
+       QPoint globalpos = trigger->parentWidget()->mapToGlobal( trigger->pos());
+       // is there enough space to put the box below the trigger?
+       if ( globalpos.y() + trigger->height()+height()+1<
+            QApplication::desktop()->height()) {
+               // is there enough space to set the box left-justified with the trigger
+               if (globalpos.x()+width()<QApplication::desktop()->width())
+                       return exec(globalpos.x(),
+                                   globalpos.y()+trigger->height()+1);
+               else
+                       return exec(globalpos.x()-width()-1,
+                                   globalpos.y()+trigger->height()+1);
+       } else {
+               if (globalpos.x()+width()<QApplication::desktop()->width())
+                       return exec(globalpos.x(),
+                                   globalpos.y()-height()-1);
+               else
+                       return exec(globalpos.x()-width()-1,
+                                   globalpos.y()-height()-1);
+       }
+}
 
+int QBrowseBox::exec(int x,int y)
+{
+       firstrelease_ = true;
+       move(x,y);
+       show();
+       repaint();
+       qApp->enter_loop();
+       inloop = true;
+       
+       if (activecell_.x()!=-1 && activecell_.y()!=-1 )
+               return coordsToIndex( activecell_.x(),activecell_.y());
+       else
+               return -1;
+}
+               
 void QBrowseBox::keyPressEvent(QKeyEvent * e)
 {
        switch(e->key()) {
@@ -149,25 +192,41 @@ void QBrowseBox::keyPressEvent(QKeyEvent * e)
                break;
        case Key_Return:
                emit selected(activecell_.x(), activecell_.y());
+               if ( isVisible() && parentWidget() &&
+                    parentWidget()->inherits("QPopupMenu") )
+                       parentWidget()->close();
+               else
+                       close();
                break;
+       case Key_Escape:
+               if (inloop)
+                       qApp->exit_loop();
+               if ( isVisible() && parentWidget() &&
+                    parentWidget()->inherits("QPopupMenu") )
+                       parentWidget()->close();
        default:
                e->ignore();
        }
 }
 
-
-void QBrowseBox::mouseReleaseEvent(QMouseEvent *)
+void QBrowseBox::contentsMouseReleaseEvent(QMouseEvent *)
 {
-       qWarning("mouse release");
-       emit selected( activecell_.x(), activecell_.y());
-}
 
+       if (firstrelease_)
+               firstrelease_ = false;
+       else {
+               emit selected( activecell_.x(), activecell_.y());
+               if ( isVisible() && parentWidget() &&
+                    parentWidget()->inherits("QPopupMenu") )
+                       parentWidget()->close();
+       }
+}
 
-//void QBrowseBox::closeEvent(QCloseEvent * e)
-//{
-//     e->accept();
-//     qApp->exit_loop();
-//}
+void QBrowseBox::closeEvent(QCloseEvent * e)
+{
+       e->accept();
+       qApp->exit_loop();
+}
 
 void QBrowseBox::paintCell(QPainter * painter, int row, int col)
 {
@@ -182,19 +241,19 @@ void QBrowseBox::paintCell(QPainter * painter, int row, int col)
        if (activecell_.x() == row && activecell_.y() == col) {
                if (ispixmap)
                        qDrawShadeRect(painter, 0, 0, cellWidth(),
-                                      cellHeight(), colorGroup(), false, 1);
+                                      cellHeight(), colorGroup(), false, 1);
                else
                        qDrawShadePanel(painter, 0, 0, cellWidth(),
-                                       cellHeight(), colorGroup(), false, 1);
+                                       cellHeight(), colorGroup(), false, 1);
        } else {
                qDrawPlainRect(painter, 0, 0, cellWidth(),
-                              cellHeight(), colorGroup().background(), 1);
+                              cellHeight(), colorGroup().background(), 1);
        }
 
        if (!texts_[coordsToIndex(row, col)].isEmpty()) {
                painter->drawText(0, 0, cellWidth(),
-                                 cellHeight(), AlignLeft,
-                                 texts_[coordsToIndex(row, col)]);
+                                 cellHeight(), AlignCenter,
+                                 texts_[coordsToIndex(row, col)]);
        }
        painter->setClipping(false);
 }
@@ -208,12 +267,11 @@ void QBrowseBox::resizeEvent(QResizeEvent * e)
 }
 
 
-void QBrowseBox::mouseMoveEvent(QMouseEvent * e)
+void QBrowseBox::contentsMouseMoveEvent(QMouseEvent * e)
 {
-       qWarning("mouseMoveEvent");
        int x = e->pos().x();
        int y = e->pos().y();
-
+       
        int cellx;
        int celly;
 
@@ -222,15 +280,11 @@ void QBrowseBox::mouseMoveEvent(QMouseEvent * e)
                cellx = -1;
                celly = -1;
        } else {
-#ifdef WITH_WARNINGS
-#warning Is the switch of x and y intended here? (Andre)
-#endif
                celly = (int)floor( ((double)x) / ((double)cellWidth()) );
                cellx = (int)floor( ((double)y) / ((double)cellHeight()) );
        }
 
        if (activecell_.x() != cellx || activecell_.y() != celly) {
-               qWarning("update");
                // mouse has been moved to another cell
                int oldactivecellx = activecell_.x();
                int oldactivecelly = activecell_.y();
@@ -250,6 +304,8 @@ void QBrowseBox::moveLeft()
 
        if (y>0)
                activecell_.setY(y - 1);
+       else if (parentWidget())
+               QWidget::focusNextPrevChild(false);
 
        updateCell(activecell_.x(), y);
        updateCell(activecell_.x(), activecell_.y());
@@ -274,6 +330,8 @@ void QBrowseBox::moveUp()
 
        if (x > 0)
                activecell_.setX(x - 1);
+       else if (parentWidget())
+               QWidget::focusNextPrevChild(false);
 
        updateCell(x, activecell_.y());
        updateCell(activecell_.x(), activecell_.y());
@@ -290,4 +348,3 @@ void QBrowseBox::moveDown()
        updateCell(x, activecell_.y());
        updateCell(activecell_.x(), activecell_.y());
 }
-