Flag.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2007 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <QMenu>
00027 # include <QMouseEvent>
00028 #endif
00029 #include <Inventor/SbVec2s.h>
00030 
00031 #include "Flag.h"
00032 
00033 using namespace Gui;
00034 
00035 #if 0 // Test functions with transparency
00036 
00037 #if 1
00038     QDialog* dlg = Gui::getMainWindow()->findChild<QDialog*>();
00039     QImage image;
00040     if (dlg) {
00041         QPixmap p = QPixmap::grabWidget(dlg);
00042         image = p.toImage();
00043     }
00044     else {
00045         QImage img(128,128, QImage::Format_ARGB32);
00046         img.fill(qRgba(255, 255, 255, 127));
00047         QPainter painter;
00048         painter.begin(&img);
00049         painter.setPen(Qt::black);
00050         painter.drawText(25, 50, QLatin1String("Hello, World!"));
00051         painter.end();
00052         image = img;
00053     }
00054 #else
00055     QPixmap pm (128,128);
00056     QBitmap mask (128,128);
00057     mask.fill(Qt::color0);
00058 
00059     QPainter painter(&mask);
00060     painter.drawText(QPoint(0, 0), QLatin1String("Hello, World!"));
00061     pm.setMask(mask);
00062 
00063     QImage img = pm.toImage();
00064     img.load(QLatin1String("C:/Temp/tux.png"),"PNG");
00065 #endif
00066 
00067 #include "MainWindow.h"
00068 void drawImage(QGLWidget* w,double x1, double y1, double x2, double y2, QImage pic)
00069 {
00070     //pic.save(QLatin1String("C:/Temp/texture.png"),"PNG");
00071 #if 0
00072     glPushAttrib(GL_ALL_ATTRIB_BITS);
00073     glEnable(GL_BLEND);
00074     glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00075     glColor4d(0.0,0.0,1.0,0.2f);
00076     glBegin(GL_QUADS);
00077     glTexCoord2d(0,0); glVertex2f(x1,y1);
00078     glTexCoord2d(1,0); glVertex2f(x2,y1);
00079     glTexCoord2d(1,1); glVertex2f(x2,y2);
00080     glTexCoord2d(0,1); glVertex2f(x1,y2);
00081     glEnd();
00082     glPopAttrib();
00083 #elif 0
00084     pic = QGLWidget::convertToGLFormat(pic);
00085     int texid = w->bindTexture(pic);
00086     glColor3f(1,1,1);
00087     glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00088     glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00089     glDisable(GL_LIGHTING);
00090     glBegin(GL_QUADS);
00091     glTexCoord2d(0,0); glVertex2f(x1,y1);
00092     glTexCoord2d(1,0); glVertex2f(x2,y1);
00093     glTexCoord2d(1,1); glVertex2f(x2,y2);
00094     glTexCoord2d(0,1); glVertex2f(x1,y2);
00095     glEnd();
00096     //    glEnable(GL_LIGHTING);
00097     w->deleteTexture(texid);
00098 #elif 0
00099     glPushAttrib(GL_ALL_ATTRIB_BITS);
00100     glEnable(GL_BLEND);
00101     glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00102     glColor4d(0.0,0.0,1.0,0.2f);
00103     int texid = w->bindTexture(pic);
00104     glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00105     glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00106     glDisable(GL_LIGHTING);
00107     glBegin(GL_QUADS);
00108     glTexCoord2d(0,0); glVertex2f(x1,y1);
00109     glTexCoord2d(1,0); glVertex2f(x2,y1);
00110     glTexCoord2d(1,1); glVertex2f(x2,y2);
00111     glTexCoord2d(0,1); glVertex2f(x1,y2);
00112     glEnd();
00113     //    glEnable(GL_LIGHTING);
00114     w->deleteTexture(texid);
00115     glPopAttrib();
00116 #elif 1
00117     glPushAttrib(GL_ALL_ATTRIB_BITS);
00118     glEnable(GL_BLEND);
00119     glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00120     glColor4d(0.0,0.0,1.0,0.2f);
00121     pic = QGLWidget::convertToGLFormat(pic);
00122     glRasterPos2d(x1,y1);
00123     glDrawPixels(pic.width(),pic.height(),GL_RGBA,GL_UNSIGNED_BYTE,pic.bits());
00124     glPopAttrib();
00125 #endif
00126 }
00127 
00128 #endif
00129 
00130 /* TRANSLATOR Gui::Flag */
00131 
00132 #if 1
00133 
00134 // TODO: Rename to Annotation
00135 //       Support transparency
00136 //       Embed complete widgets
00137 
00138 Flag::Flag(QWidget* parent)
00139   : QGLWidget(parent), coord(0.0f, 0.0f, 0.0f)
00140 {
00141     this->setFixedHeight(20);
00142 }
00143 
00144 void Flag::initializeGL()
00145 {
00146     const QPalette& p = this->palette();
00147     qglClearColor(/*Qt::white*/p.color(QPalette::Window));
00148 }
00149 
00150 void Flag::paintGL()
00151 {
00152     const QPalette& p = this->palette();
00153     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00154     qglColor(/*Qt::black*/p.color(QPalette::Text));
00155     renderText(10,15,this->text);
00156 }
00157 
00158 void Flag::resizeGL(int width, int height)
00159 {
00160     return;
00161     //int side = qMin(width, height);
00162     //glViewport((width - side) / 2, (height - side) / 2, side, side);
00163 
00164     //glMatrixMode(GL_PROJECTION);
00165     //glLoadIdentity();
00166     //glFrustum(-1.0, +1.0, -1.0, 1.0, 5.0, 60.0);
00167     //glMatrixMode(GL_MODELVIEW);
00168     //glLoadIdentity();
00169     //glTranslated(0.0, 0.0, -40.0);
00170 }
00171 
00172 #else
00173 
00174 Flag::Flag(QWidget* parent)
00175   : QWidget(parent), coord(0.0f, 0.0f, 0.0f)
00176 {
00177     this->setFixedHeight(20);
00178 }
00179 #endif
00180 
00181 Flag::~Flag()
00182 {
00183 }
00184 
00185 void Flag::setOrigin(const SbVec3f& v)
00186 {
00187     this->coord = v;
00188 }
00189 
00190 const SbVec3f& Flag::getOrigin() const
00191 {
00192     return this->coord;
00193 }
00194 
00195 void Flag::drawLine (int tox, int toy)
00196 {
00197     if (!isVisible())
00198         return;
00199 
00200     // Get position of line
00201     QSize s = parentWidget()->size();
00202     SbVec2s view(s.width(), s.height());
00203     int fromx = pos().x();
00204     int fromy = pos().y() + height()/2;
00205     if (false) fromx += width();
00206 
00207     glMatrixMode(GL_PROJECTION);
00208     glPushMatrix();
00209     glLoadIdentity();
00210     glOrtho(0, view[0], 0, view[1], -1, 1);
00211 
00212     // Store GL state
00213     glPushAttrib(GL_ALL_ATTRIB_BITS);
00214     GLfloat depthrange[2];
00215     glGetFloatv(GL_DEPTH_RANGE, depthrange);
00216     GLdouble projectionmatrix[16];
00217     glGetDoublev(GL_PROJECTION_MATRIX, projectionmatrix);
00218 
00219     glDepthFunc(GL_ALWAYS);
00220     glDepthMask(GL_TRUE);
00221     glDepthRange(0,0);
00222     glEnable(GL_DEPTH_TEST);
00223     glDisable(GL_LIGHTING);
00224     glEnable(GL_COLOR_MATERIAL);
00225     glDisable(GL_BLEND);
00226 
00227     glColor4f(1.0, 1.0, 1.0, 0.0);
00228     glViewport(0, 0, view[0], view[1]);
00229 
00230     // the line
00231     glLineWidth(1.0f);
00232     glBegin(GL_LINE_LOOP);
00233         glVertex3i(fromx, view[1]-fromy, 0);
00234         glVertex3i(tox  , view[1]-toy  , 0);
00235     glEnd();
00236 
00237     glPointSize(3.0f);
00238     glBegin(GL_POINTS);
00239         glVertex3i(tox  , view[1]-toy  , 0);
00240     glEnd();
00241 
00242     glFlush();
00243 
00244     // Reset original state
00245     glDepthRange(depthrange[0], depthrange[1]);
00246     glMatrixMode(GL_PROJECTION);
00247     glLoadMatrixd(projectionmatrix);
00248 
00249     glPopAttrib();
00250     glPopMatrix();
00251 }
00252 
00253 void Flag::setText(const QString& t)
00254 {
00255     this->text = t;
00256 }
00257 
00258 void Flag::resizeEvent(QResizeEvent * e)
00259 {
00260 #if 0
00261     image = QImage(this->size(), QImage::Format_ARGB32);
00262 
00263     QPainter painter;
00264     painter.begin(&image);
00265     painter.fillRect(image.rect(), Qt::white);
00266     painter.setPen(Qt::black);
00267     painter.drawText(10, 15, this->text);
00268     painter.end();
00269 #endif
00270 }
00271 
00272 void Flag::paintEvent(QPaintEvent* e)
00273 {
00274 #if 1
00275     QGLWidget::paintEvent(e);
00276 #else
00277 #if 1
00278     QPainter painter;
00279     painter.begin(this);
00280     painter.setRenderHint(QPainter::Antialiasing);
00281 
00282     painter.drawImage((width() - image.width())/2, 0, image);
00283     painter.end();
00284 #else
00285     // draw the overlayed text using QPainter
00286     QPainter p(this);
00287     p.fillRect(this->rect(), Qt::white);
00288     p.setPen(Qt::black);
00289     p.setBrush(Qt::NoBrush);
00290     QFontMetrics fm(p.font());
00291     p.drawText(10, 15, this->text);
00292 #endif
00293 #endif
00294 }
00295 
00296 void Flag::mouseMoveEvent(QMouseEvent *e)
00297 {
00298     if (e->buttons() & Qt::LeftButton) {
00299         move(e->globalPos() - dragPosition);
00300         e->accept();
00301     }
00302 }
00303 
00304 void Flag::mousePressEvent(QMouseEvent *e)
00305 {
00306     if (e->button() == Qt::LeftButton) {
00307         dragPosition = e->globalPos() - frameGeometry().topLeft();
00308         e->accept();
00309     }
00310 }
00311 
00312 void Flag::contextMenuEvent(QContextMenuEvent * e)
00313 {
00314     QMenu menu(this);
00315 
00316     QAction* topleft = menu.addAction(tr("Top left"));
00317     topleft->setCheckable(true);
00318     QAction* botleft = menu.addAction(tr("Bottom left"));
00319     botleft->setCheckable(true);
00320     QAction* topright = menu.addAction(tr("Top right"));
00321     topright->setCheckable(true);
00322     QAction* botright = menu.addAction(tr("Bottom right"));
00323     botright->setCheckable(true);
00324     menu.addSeparator();
00325     QAction* remove = menu.addAction(tr("Remove"));
00326     QAction* select = menu.exec(e->globalPos());
00327     if (remove == select)
00328         this->deleteLater();
00329 }
00330 
00331 QSize Flag::sizeHint() const
00332 {
00333     int w = 100;
00334     int h = 20;
00335     QFontMetrics metric(this->font());
00336     QRect r = metric.boundingRect(text);
00337     w = std::max<int>(w, r.width()+20);
00338     h = std::max<int>(h, r.height());
00339     return QSize(w, h);
00340 }
00341 
00342 // ------------------------------------------------------------------------
00343 
00344 FlagLayout::FlagLayout(QWidget *parent, int margin, int spacing)
00345     : QLayout(parent)
00346 {
00347     setMargin(margin);
00348     setSpacing(spacing);
00349 }
00350 
00351 FlagLayout::FlagLayout(int spacing)
00352 {
00353     setSpacing(spacing);
00354 }
00355 
00356 FlagLayout::~FlagLayout()
00357 {
00358     QLayoutItem *l;
00359     while ((l = takeAt(0)))
00360         delete l;
00361 }
00362 
00363 void FlagLayout::addItem(QLayoutItem *item)
00364 {
00365     add(item, TopLeft);
00366 }
00367 
00368 void FlagLayout::addWidget(QWidget *widget, Position position)
00369 {
00370     add(new QWidgetItem(widget), position);
00371 }
00372 
00373 Qt::Orientations FlagLayout::expandingDirections() const
00374 {
00375     return Qt::Horizontal | Qt::Vertical;
00376 }
00377 
00378 bool FlagLayout::hasHeightForWidth() const
00379 {
00380     return false;
00381 }
00382 
00383 int FlagLayout::count() const
00384 {
00385     return list.size();
00386 }
00387 
00388 QLayoutItem *FlagLayout::itemAt(int index) const
00389 {
00390     ItemWrapper *wrapper = list.value(index);
00391     if (wrapper)
00392         return wrapper->item;
00393     else
00394         return 0;
00395 }
00396 
00397 QSize FlagLayout::minimumSize() const
00398 {
00399     return calculateSize(MinimumSize);
00400 }
00401 
00402 void FlagLayout::setGeometry(const QRect &rect)
00403 {
00404     int topHeight = 0;
00405     int bottomHeight = 0;
00406 
00407     QLayout::setGeometry(rect);
00408 
00409     // left side
00410     for (int i = 0; i < list.size(); ++i) {
00411         ItemWrapper *wrapper = list.at(i);
00412         QLayoutItem *item = wrapper->item;
00413         Position position = wrapper->position;
00414 
00415         if (position == TopLeft) {
00416             topHeight += spacing();
00417             item->setGeometry(QRect(rect.x() + spacing(), topHeight, 
00418                                     item->sizeHint().width(), item->sizeHint().height()));
00419 
00420             topHeight += item->geometry().height();
00421         } else if (position == BottomLeft) {
00422             bottomHeight += item->geometry().height() + spacing();
00423             item->setGeometry(QRect(rect.x() + spacing(), rect.height() - bottomHeight, 
00424                                     item->sizeHint().width(), item->sizeHint().height()));
00425         }
00426     }
00427 
00428     // right side
00429     topHeight = 0;
00430     bottomHeight = 0;
00431     for (int i = 0; i < list.size(); ++i) {
00432         ItemWrapper *wrapper = list.at(i);
00433         QLayoutItem *item = wrapper->item;
00434         Position position = wrapper->position;
00435 
00436         int rightpos = item->sizeHint().width() + spacing();
00437         if (position == TopRight) {
00438             topHeight += spacing();
00439             item->setGeometry(QRect(rect.x() + rect.width() - rightpos, topHeight, 
00440                                     item->sizeHint().width(), item->sizeHint().height()));
00441 
00442             topHeight += item->geometry().height();
00443         } else if (position == BottomRight) {
00444             bottomHeight += item->geometry().height() + spacing();
00445             item->setGeometry(QRect(rect.x() + rect.width() - rightpos, rect.height() - bottomHeight, 
00446                                     item->sizeHint().width(), item->sizeHint().height()));
00447         }
00448     }
00449 }
00450 
00451 QSize FlagLayout::sizeHint() const
00452 {
00453     return calculateSize(SizeHint);
00454 }
00455 
00456 QLayoutItem *FlagLayout::takeAt(int index)
00457 {
00458     if (index >= 0 && index < list.size()) {
00459         ItemWrapper *layoutStruct = list.takeAt(index);
00460         return layoutStruct->item;
00461     }
00462     return 0;
00463 }
00464 
00465 void FlagLayout::add(QLayoutItem *item, Position position)
00466 {
00467     list.append(new ItemWrapper(item, position));
00468 }
00469 
00470 QSize FlagLayout::calculateSize(SizeType sizeType) const
00471 {
00472     QSize totalSize;
00473 
00474     for (int i = 0; i < list.size(); ++i) {
00475         ItemWrapper *wrapper = list.at(i);
00476         QSize itemSize;
00477 
00478         if (sizeType == MinimumSize)
00479             itemSize = wrapper->item->minimumSize();
00480         else // (sizeType == SizeHint)
00481             itemSize = wrapper->item->sizeHint();
00482 
00483         totalSize.rheight() += itemSize.height();
00484         totalSize.rwidth() = qMax<int>(totalSize.width(),itemSize.width());
00485     }
00486     return totalSize;
00487 }
00488 
00489 #include "moc_Flag.cpp"

Generated on Wed Nov 23 19:00:14 2011 for FreeCAD by  doxygen 1.6.1