kword/part

KWFrameGeometry.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002  * Copyright (C) 2006-2007 Thomas Zander <zander@kde.org>
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this library; see the file COPYING.LIB.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  */
00019 #include "KWFrameGeometry.h"
00020 #include "KWDocument.h"
00021 #include "frames/KWTextFrame.h"
00022 #include "frames/KWTextFrameSet.h"
00023 
00024 #include <KoShape.h>
00025 
00026 #include <kdebug.h>
00027 
00028 KWFrameGeometry::KWFrameGeometry(FrameConfigSharedState *state)
00029         : m_state(state),
00030         m_frame(0),
00031         m_blockSignals(false)
00032 {
00033     m_state->addUser();
00034     widget.setupUi(this);
00035     setUnit(m_state->document()->unit());
00036     widget.xPos->setMinimum(0.0);
00037     widget.yPos->setMinimum(0.0);
00038     widget.width->setMinimum(0.0);
00039     widget.height->setMinimum(0.0);
00040     widget.leftMargin->setMinimum(0.0);
00041     widget.rightMargin->setMinimum(0.0);
00042     widget.bottomMargin->setMinimum(0.0);
00043     widget.topMargin->setMinimum(0.0);
00044 
00045     widget.keepAspect->setKeepAspectRatio(m_state->keepAspectRatio());
00046 
00047     connect(widget.leftMargin, SIGNAL(valueChangedPt(qreal)), this, SLOT(syncMargins(qreal)));
00048     connect(widget.rightMargin, SIGNAL(valueChangedPt(qreal)), this, SLOT(syncMargins(qreal)));
00049     connect(widget.bottomMargin, SIGNAL(valueChangedPt(qreal)), this, SLOT(syncMargins(qreal)));
00050     connect(widget.topMargin, SIGNAL(valueChangedPt(qreal)), this, SLOT(syncMargins(qreal)));
00051 
00052     connect(widget.width, SIGNAL(valueChangedPt(qreal)), this, SLOT(widthChanged(qreal)));
00053     connect(widget.height, SIGNAL(valueChangedPt(qreal)), this, SLOT(heightChanged(qreal)));
00054 
00055     connect(m_state, SIGNAL(keepAspectRatioChanged(bool)), widget.keepAspect, SLOT(setKeepAspectRatio(bool)));
00056     connect(widget.keepAspect, SIGNAL(keepAspectRatioChanged(bool)), this, SLOT(updateAspectRatio(bool)));
00057 
00058     connect(widget.positionSelector, SIGNAL(positionSelected(KoFlake::Position)),
00059             this, SLOT(setGeometryAlignment(KoFlake::Position)));
00060 
00061 }
00062 
00063 KWFrameGeometry::~KWFrameGeometry()
00064 {
00065     m_state->removeUser();
00066 }
00067 
00068 void KWFrameGeometry::open(KWFrame *frame)
00069 {
00070     m_frame = frame;
00071     open(frame->shape());
00072     // TODO rest
00073 }
00074 
00075 void KWFrameGeometry::open(KoShape *shape)
00076 {
00077     m_originalPosition = shape->absolutePosition();
00078     m_originalSize = shape->size();
00079     QPointF position = shape->absolutePosition(widget.positionSelector->position());
00080     widget.xPos->changeValue(position.x());
00081     widget.yPos->changeValue(position.y());
00082     widget.width->changeValue(m_originalSize.width());
00083     widget.height->changeValue(m_originalSize.height());
00084 
00085     connect(widget.protectSize, SIGNAL(stateChanged(int)),
00086             this, SLOT(protectSizeChanged(int)));
00087 
00088     if (shape->isGeometryProtected()) {
00089         widget.protectSize->setCheckState(Qt::Checked);
00090         KWTextFrame *tf = dynamic_cast<KWTextFrame*>(shape->applicationData());
00091         if (tf && static_cast<KWTextFrameSet*>(tf->frameSet())->textFrameSetType() != KWord::OtherTextFrameSet)
00092             widget.protectSize->setEnabled(false); // auto-generated frame, can't edit
00093     }
00094 
00095     connect(widget.xPos, SIGNAL(valueChanged(double)), this, SLOT(updateShape()));
00096     connect(widget.yPos, SIGNAL(valueChanged(double)), this, SLOT(updateShape()));
00097     connect(widget.width, SIGNAL(valueChanged(double)), this, SLOT(updateShape()));
00098     connect(widget.height, SIGNAL(valueChanged(double)), this, SLOT(updateShape()));
00099 }
00100 
00101 void KWFrameGeometry::updateShape()
00102 {
00103     if (m_blockSignals) return;
00104     KWFrame *frame = m_frame;
00105     if (frame == 0) {
00106         frame = m_state->frame();
00107         m_state->markFrameUsed();
00108     }
00109     Q_ASSERT(frame);
00110     frame->shape()->update();
00111     QPointF pos(widget.xPos->value(), widget.yPos->value());
00112     frame->shape()->setAbsolutePosition(pos, widget.positionSelector->position());
00113     QSizeF size(widget.width->value(), widget.height->value());
00114     frame->shape()->setSize(size);
00115     frame->shape()->update();
00116 }
00117 
00118 void KWFrameGeometry::protectSizeChanged(int protectSizeState)
00119 {
00120     KWFrame *frame = m_frame;
00121     if (frame == 0) {
00122         frame = m_state->frame();
00123         m_state->markFrameUsed();
00124     }
00125     Q_ASSERT(frame);
00126     bool lock = (protectSizeState == Qt::Checked);
00127     frame->shape()->setGeometryProtected(lock);
00128     widget.xPos->setDisabled(lock);
00129     widget.yPos->setDisabled(lock);
00130     widget.width->setDisabled(lock);
00131     widget.height->setDisabled(lock);
00132     widget.keepAspect->setDisabled(lock);
00133 }
00134 
00135 void KWFrameGeometry::syncMargins(qreal value)
00136 {
00137     if (! widget.synchronize->isChecked())
00138         return;
00139 
00140     widget.leftMargin->changeValue(value);
00141     widget.topMargin->changeValue(value);
00142     widget.rightMargin->changeValue(value);
00143     widget.bottomMargin->changeValue(value);
00144 }
00145 
00146 void KWFrameGeometry::save()
00147 {
00148     // no-op now
00149 }
00150 
00151 void KWFrameGeometry::cancel()
00152 {
00153     KWFrame *frame = m_frame;
00154     if (frame == 0) {
00155         frame = m_state->frame();
00156         m_state->markFrameUsed();
00157     }
00158     Q_ASSERT(frame);
00159     frame->shape()->setAbsolutePosition(m_originalPosition);
00160     frame->shape()->setSize(m_originalSize);
00161 }
00162 
00163 void KWFrameGeometry::widthChanged(qreal value)
00164 {
00165     if (! m_state->keepAspectRatio())  return;
00166     if (m_blockSignals) return;
00167     m_blockSignals = true;
00168     widget.height->changeValue(m_originalSize.height() / m_originalSize.width() * value);
00169     m_blockSignals = false;
00170 }
00171 
00172 void KWFrameGeometry::heightChanged(qreal value)
00173 {
00174     if (! m_state->keepAspectRatio())  return;
00175     if (m_blockSignals) return;
00176     m_blockSignals = true;
00177     widget.width->changeValue(m_originalSize.width() / m_originalSize.height() * value);
00178     m_blockSignals = false;
00179 }
00180 
00181 void KWFrameGeometry::setUnit(KoUnit unit)
00182 {
00183     widget.xPos->setUnit(unit);
00184     widget.yPos->setUnit(unit);
00185     widget.width->setUnit(unit);
00186     widget.height->setUnit(unit);
00187     widget.leftMargin->setUnit(unit);
00188     widget.topMargin->setUnit(unit);
00189     widget.rightMargin->setUnit(unit);
00190     widget.bottomMargin->setUnit(unit);
00191 }
00192 
00193 void KWFrameGeometry::setGeometryAlignment(KoFlake::Position position)
00194 {
00195     QPointF pos = m_frame->shape()->absolutePosition(position);
00196     m_blockSignals = true;
00197     widget.xPos->changeValue(pos.x());
00198     widget.yPos->changeValue(pos.y());
00199     m_blockSignals = false;
00200 }
00201 
00202 void KWFrameGeometry::updateAspectRatio(bool keep)
00203 {
00204     m_state->setKeepAspectRatio(keep);
00205     if (keep)
00206         widget.height->changeValue(m_originalSize.height() / m_originalSize.width() * widget.width->value());
00207 }
00208