STK++ 1.0
STK_Connection.cpp
Go to the documentation of this file.
00001 /*--------------------------------------------------------------------*/
00002 /*     Copyright (C) 2004-2007  Serge Iovleff
00003 
00004  This program is free software; you can redistribute it and/or modify
00005  it under the terms of the GNU Lesser General Public License as
00006  published by the Free Software Foundation; either version 2 of the
00007  License, or (at your option) any later version.
00008 
00009  This program 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
00012  GNU Lesser General Public License for more details.
00013 
00014  You should have received a copy of the GNU Lesser General Public
00015  License along with this program; if not, write to the
00016  Free Software Foundation, Inc.,
00017  59 Temple Place,
00018  Suite 330,
00019  Boston, MA 02111-1307
00020  USA
00021 
00022  Contact : Serge.Iovleff@stkpp.org
00023  */
00024 
00025 /*
00026  * Project:  stkpp::gui::model
00027  * created on: 2 oct. 2009
00028  * Purpose:  implement the Connection class.
00029  * Author:   iovleff, serge.iovleff@stkpp.org
00030  **/
00031 
00036 #include <stdexcept>
00037 
00038 #include "../include/STK_Connection.h"
00039 #include "../include/STK_IStep.h"
00040 
00041 namespace STK
00042 {
00043 
00044 /* Id Properties */
00045 const String Connection::ID_CONNECTION_LINESTYLE("Connection.LineStyle");
00046 const String Connection::ID_CONNECTION_SOURCE("Connection.Source");
00047 const String Connection::ID_CONNECTION_TARGET("Connection.Target");
00048 
00049 
00050 /* Property Descriptors */
00051 const PropertyDescriptor Connection::lineStyleDescriptor_( Connection::ID_CONNECTION_LINESTYLE
00052                                                           , "Line");
00053 const PropertyDescriptor Connection::sourceDescriptor_( Connection::ID_CONNECTION_SOURCE
00054                                                        , "Source Step");
00055 const PropertyDescriptor Connection::targetDescriptor_( Connection::ID_CONNECTION_TARGET
00056                                                        , "Target Step");
00057 
00058 // Values of the LineStyle Property
00059 const String Connection::lineStyleName[] =
00060 {
00061   "No Line",
00062   "Solid Line",
00063   "Dash Line",
00064   "Dot Line",
00065   "Dash Dot Line",
00066   "Dash Dot Dot Line",
00067   "Custom Dash Line"
00068 };
00069 
00070 
00071 /* constructor */
00072 Connection::Connection( IStep* source
00073                       , IStep* target
00074                       , const PenStyle& lineStyle
00075                       )
00076                       : lineStyle_(lineStyle)
00077                       , source_(source)
00078                       , target_(target)
00079 { ;}
00080 
00081 /* destructor. */
00082 Connection::~Connection()
00083 { ;}
00084 
00085 /*
00086  * Returns the line drawing style of this connection.
00087  * @return a Qt::PenStyle
00088  */
00089 const Connection::PenStyle& Connection::getLineStyle() const
00090 { return lineStyle_;}
00091 
00095 IStep* Connection::getSource() const
00096 { return source_;}
00097 
00101 IStep* Connection::getTarget() const
00102 { return target_;}
00103 
00104 
00110 void Connection::setLineStyle(const PenStyle& lineStyle)
00111 {
00112   if (lineStyle == NoPen)
00113   {
00114     throw std::invalid_argument("In Connection::setLineStyle : try to set"
00115                                 "NoPen style");
00116   }
00117   lineStyle_ = lineStyle;
00118 }
00119 
00120 /*
00121  * Disconnect this connection from the ISteps it is attached to.
00122  */
00123 void Connection::disconnect()
00124 {
00125   if (isConnected_)
00126   {
00127     source_->removeConnection(this);
00128     target_->removeConnection(this);
00129     isConnected_ = false;
00130   }
00131 }
00132 
00133 /* Reconnect this connection.
00134  * The connection will reconnect with the steps it was previously
00135  * attached to.
00136  */
00137 void Connection::reconnect()
00138 {
00139   if (!isConnected_)
00140   {
00141     source_->addConnection(this);
00142     target_->addConnection(this);
00143     isConnected_ = true;
00144   }
00145 }
00146 
00147 /* Reconnect to a different source and/or target Step.
00148  * he connection will disconnect from its current attachments and
00149  * reconnect to the new source and target.
00150  */
00151 void Connection::reconnect( IStep* newSource
00152                           , IStep* newTarget
00153                           )
00154 {
00155   if (newSource == newTarget)
00156   {
00157     throw std::invalid_argument("STK::Connection::reconnect(newSource, newTarget"
00158                                 " source and target must be different"
00159                                );
00160   }
00161   disconnect();
00162   this->source_ = newSource;
00163   this->target_ = newTarget;
00164   reconnect();
00165 }
00166 
00167 /* @brief Implementation of the pure virtual method
00168  *  <code> isPropertySet</code> defined in <code> IPropertySource</code>
00169  * @see IPropertySource#isPropertySet
00170  * @return <code>true</code> if id is a valid Connection id property.
00171  */
00172 bool Connection::isPropertySet(String const& id) const
00173 {
00174   if (id==ID_CONNECTION_LINESTYLE) return true;
00175   if ((id==ID_CONNECTION_SOURCE||id==ID_CONNECTION_TARGET)&&
00176       (isConnected_)) return true;
00177   return false;
00178 }
00179 
00180 /* @brief Implementation of the pure virtual method
00181  *  <code> getPropertyValue</code> defined in <code> IPropertySource</code>
00182  * @see IPropertySource#getPropertValue
00183  *
00184  * @param id
00185  *            the id of the property being set
00186  * @return the value of the property, or <code>NULL</code>
00187  */
00188 const String* Connection::getPropertyValue(String const& id) const
00189 {
00190   // line  style name
00191   if (id==ID_CONNECTION_LINESTYLE)
00192     return &(lineStyleName[lineStyle_]);
00193   // name of the source
00194   if ((id==ID_CONNECTION_SOURCE)&& isConnected_)
00195     return &(source_->name());
00196   // name of the target
00197   if ((id==ID_CONNECTION_TARGET)&& isConnected_)
00198     return &(target_->name());
00199   return NULL;
00200 }
00201 
00202 } /* namespace STK */