File indexing completed on 2024-12-22 04:17:35

0001 /***************************************************************************
0002  *                                                                         *
0003  *   copyright : (C) 2008 The University of Toronto                        *
0004  *                   netterfield@astro.utoronto.ca                         *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  ***************************************************************************/
0012 
0013 #include "lineitemdialog.h"
0014 
0015 #include "linedimensionstab.h"
0016 #include "lineitem.h"
0017 #include "dialogpage.h"
0018 #include "application.h"
0019 #include "objectstore.h"
0020 #include "mainwindow.h"
0021 #include "document.h"
0022 
0023 namespace Kst {
0024 
0025 LineItemDialog::LineItemDialog(LineItem *item, QWidget *parent)
0026   : ViewItemDialog(item, parent), _viewItem(item) {
0027   _lineDimensionsTab = new LineDimensionsTab(item, this);
0028 
0029   DialogPage *lineDimensionsPage = new DialogPage(this);
0030   lineDimensionsPage->setPageTitle(tr("Size/Position"));
0031   lineDimensionsPage->addDialogTab(_lineDimensionsTab);
0032   addDialogPage(lineDimensionsPage);
0033   selectDialogPage(lineDimensionsPage);
0034 
0035   connect(_lineDimensionsTab, SIGNAL(apply()), this, SLOT(dimensionsChanged()));
0036 
0037   setupDimensions();
0038 
0039   connect(_lineDimensionsTab, SIGNAL(tabModified()), this, SLOT(modified()));
0040 
0041 }
0042 
0043 void LineItemDialog::setupDimensions() {
0044   _lineDimensionsTab->enableSingleEditOptions(true);
0045   _lineDimensionsTab->setupDimensions();
0046 }
0047 
0048 void LineItemDialog::saveDimensions(ViewItem *item) {
0049   Q_ASSERT(item);
0050 
0051   qreal x1 = _lineDimensionsTab->x1();
0052   qreal y1 = _lineDimensionsTab->y1();
0053   qreal x2 = _lineDimensionsTab->x2();
0054   qreal y2 = _lineDimensionsTab->y2();
0055 
0056   if (_lineDimensionsTab->lockPosToData() && item->dataPosLockable()) {
0057     QRectF dr;
0058     dr.setTopLeft(QPointF(x1,y1));
0059     dr.setBottomRight(QPointF(x2,y2));
0060 
0061     item->setDataRelativeRect(dr);
0062 
0063     bool lockPosToData = _lineDimensionsTab->lockPosToDataDirty() ? _lineDimensionsTab->lockPosToData() : item->lockPosToData();
0064     item->setLockPosToData(lockPosToData);
0065 
0066     item->applyDataLockedDimensions();
0067   } else {
0068     QRectF parentRect = item->parentRect();
0069     qreal parentWidth = parentRect.width();
0070     qreal parentHeight = parentRect.height();
0071     qreal parentX = parentRect.x();
0072     qreal parentY = parentRect.y();
0073 
0074     x1 = x1*parentWidth + parentX;
0075     x2 = x2*parentWidth + parentX;
0076     y1 = y1*parentHeight + parentY;
0077     y2 = y2*parentHeight + parentY;
0078     qreal dx = x2-x1;
0079     qreal dy = y2-y1;
0080 
0081     qreal w = sqrt(dx*dx + dy*dy);
0082     qreal h = item->height();
0083 
0084     item->setPos(x1,y1);
0085     item->setViewRect(0,-h/2,w,h);
0086 
0087     qreal rotation = atan2(dy,dx);
0088 
0089     QTransform transform;
0090     transform.rotateRadians(rotation);
0091 
0092     item->setTransform(transform);
0093     item->updateRelativeSize();
0094     item->setLockPosToData(false);
0095   }
0096 }
0097 }