gapMea  1.0-rc1-1.5.3.2
GapMea est un outil visuel écrit en c++ à l'aide de la bibliothèque QT qui sert de base entre autre à l'environnement graphique de KDE.Il permet de dessiner des schémas recueillant et structurant les informations nécessaires à un domaine de gestion. Le schéma obtenu est enregistré dans un fichier texte au format xml.
 All Classes Functions Variables
entite.cpp
1 #include "entite.h"
2 #include <QFontMetrics>
3 #include <QAction>
4 #include <QGraphicsWidget>
5 #include<QGraphicsSceneContextMenuEvent>
6 #include <QGraphicsItem>
7 #include <QMenu>
8 #include <QDebug>
9 #include "field.h"
10 #include <QInputDialog>
11 #include "mainwindow.h"
12 #include "lien.h"
13 #include "association.h"
14 
15 
16 class Property;
17 class Lien;
18 class Association;
19 
20 Entite::~Entite()
21 {
22  qDebug()<<"Entite::~Entite()";
23  if(this->rectTitle!=NULL)
24  {
25  delete this->rectTitle;
26  rectTitle=NULL;
27  }
28  else
29  {
30  delete this->title;
31  this->title=NULL;
32  }
33 
34  //On n'efface pas les champs parce qu'ils ont pour parent l'entité et qu'ils sont donc supprimés automatiquement
35 
36  //effacement des liens qui partent ou arrivent à l'objet
37  foreach(Lien* leLien,vectLiens)
38  {
39  maman->supprimerLien(leLien);
40  }
41 }
42 
43 void Entite::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
44 {
45  QGraphicsRectItem::paint(painter,option,0);
46  foreach(Lien* unLien, vectLiens)
47  {
48  unLien->QGraphicsItemGroup::update(unLien->boundingRect());
49  }
50 }
51 
53 {
54  //calcul de la largeur et de la hauteur;
55  long hauteur=20+QFontMetrics(title->font()).height()*vecteurChamps.size()*1.5;
56  long largeur=QFontMetrics(title->font()).width(nomTableTronque())+20;
57  for(int noChamp=0;noChamp<vecteurChamps.count();noChamp++)
58  {
59  long largeurDuChamp=QFontMetrics(title->font()).width(vecteurChamps[noChamp]->getNomComplet())+20;
60  if (largeurDuChamp>largeur) largeur=largeurDuChamp;
61  }
62  //le gros rectangle jaune de l'entité
63  setRect(0,0,largeur,hauteur);
64  setBrush(QColor("#FFFFA4"));//jaune
65  //le titre
66  if(!association)
67  {
68  rectTitle->setRect(0,0,largeur,QFontMetrics(title->font()).height()+10);
69  rectTitle->setBrush(QColor("#D6FF9A"));//vert
70  rectTitle->setPos(0,0);
71  }
72  //le texte du titre
73  title->setPos(0,0);//par rapport à son parent
74  title->setTextWidth(boundingRect().width());
75  title->setHtml("<center>"+nomTableTronque()+"</center>");
76 
77  //les champs
78  int ordonnee=0;
79  if(!association)ordonnee=rectTitle->rect().height();
80  else ordonnee=title->boundingRect().height();
81  for(int noChamp=0;noChamp<vecteurChamps.count();noChamp++)
82  {
83  vecteurChamps[noChamp]->setPos(10,ordonnee);
84  QString texte;
85  if(noChamp==0 && !association)
86  {
87  texte="<b><u>"+vecteurChamps[noChamp]->getNomComplet()+"</u></b>";
88  }
89  else texte=vecteurChamps[noChamp]->getNomComplet();
90 
91  vecteurChamps[noChamp]->setHtml(texte);
92  ordonnee+=vecteurChamps[noChamp]->boundingRect().height();
93  }
94 
95 }
96 int Entite::noLien(Entite* lAutre, Lien* unLien)
97 {
98  int resultat=0;
99  foreach (Lien* leLien, vectLiens)
100  {
101  if((leLien->t1==this && leLien->t2==lAutre) ||(leLien->t2==this && leLien->t1==lAutre)){
102  resultat++;
103  if(leLien==unLien)
104  return resultat;
105  }
106  }
107  return resultat;
108 }
109 int Entite::nbLien(Entite* lAutre)
110 {
111  int resultat=0;
112  foreach (Lien* leLien, vectLiens)
113  {
114  if((leLien->t1==this && leLien->t2==lAutre) ||(leLien->t2==this && leLien->t1==lAutre)){
115  resultat++;
116  }
117  }
118  return resultat;
119 }
120 Entite::Entite(MainWindow* mum,QString nom,qreal x,qreal y, QGraphicsItem* parent, QCustomGraphicsScene * laScene,QStringList listeDesChamps,bool isAssoc)
121  :maman(mum),QGraphicsRectItem(x,y,200,200,parent), QListWidgetItem(nom)
122 
123 {
124 qDebug()<<"constructeur de table";
125 association=isAssoc;
126  nomEntite=nom;
127  //le titre
128  if(!association)
129  {
130  rectTitle=new QGraphicsRectItem(this);
132  }
133  else
134  {
135  title=new QGraphicsTextItem(this);
136  }
137  //police du titre
138  title->setFont(QFont("Verdana",10,QFont::Bold,false));
139  //déterminons la largeur de la table ainsi que sa hauteur:
140  long hauteur=20;//marge haut + basse
141  hauteur+=QFontMetrics(title->font()).height();
142  int marge=20;
143  long largeurMaxi=QFontMetrics(title->font()).width(nomTableTronque()+marge);
144  for(int noChamp=0;noChamp<listeDesChamps.count();noChamp++)
145  {
146  long largeurDuChamp=QFontMetrics(title->font()).width(listeDesChamps[noChamp]+nomEntite);
147  hauteur+=QFontMetrics(title->font()).height();
148  if (largeurDuChamp>largeurMaxi) largeurMaxi=largeurDuChamp;
149  }
150 
151  //construction du QGraphicsRectItem
152  setRect(x,y,largeurMaxi+10,200);
153 
154 
155  //création du titre
156  setBrush(QColor("#FFFF00"));//jaune
157  title->setPos(0,0);//par rapport à son parent
158  title->setTextWidth(boundingRect().width());
159  title->setHtml("<center>"+nomTableTronque()+"</center>");
160 
161  //stockage de quelques infos
162  //c'est une table
163  title->setData(32,"Table");
164  title->setData(34,qVariantFromValue((qlonglong)this));
165  //on stocke son nom
166  title->setData(33,nomEntite);
167  QGraphicsItem::setData(32,"Table");
168  QGraphicsItem::setData(33,nomEntite);
169  QGraphicsItem::setData(34,qVariantFromValue((qlonglong)this));
170  long ordonne=title->boundingRect().height();
171 
172  this->setRect(0,0,largeurMaxi+10,ordonne+10);
173  //création des champs dans la table
174  for(int noChamp=0;noChamp<listeDesChamps.count();noChamp++)
175  {
176  QString tipe="VARCHAR";
177  QString taille="25";
178  QString role="";
179  if(noChamp==0)
180  {
181  tipe="INTEGER";
182  taille="";
183  role="Identifiant de "+nomEntite;
184  }
185 
186 
187  vecteurChamps.push_back(new Property(maman,false,laScene,listeDesChamps[noChamp]+nomEntite,tipe,role,taille,this));
188  vecteurChamps[noChamp]->setPos(10,ordonne);
189  //sa table c'est moi
190  vecteurChamps[noChamp]->lEntite=this;
191  //vecteurChamps[noChamp]->setFlag(QGraphicsItem::ItemIsSelectable,true);
192  vecteurChamps[noChamp]->setData(32,"Field");
193  vecteurChamps[noChamp]->setData(33,nom+"."+listeDesChamps[noChamp]);
194  ordonne+=vecteurChamps[noChamp]->boundingRect().height();
195 
196  }
197  setRect(0,0,largeurMaxi+20,ordonne);
198  setAcceptDrops(false);
199 
200 }
201 
202 
203 
204  void Entite::contextMenuEvent(QGraphicsSceneMouseEvent *event)
205  {
206  //ce qui se passe lorsque le menu contextuel de la table est appelé
207  qDebug()<<"void Entite::contextMenuEvent(QGraphicsSceneMouseEvent *event)";
208  QMenu menu(QObject::tr("Object Menu"));
209  //ajout du titre
210 
211  QAction* titre=new QAction(menu.title(),this->maman);
212  titre->setDisabled(true);
213  menu.addAction(titre);
214 
215  titre->setFont(QFont("verdana",9,3,true));
216 
217 
218  //si je n'était pas sélectionnée, je le deviens
219  QGraphicsItem::setSelected(true);
220  //selection de l'objet
221  maman->selectionne(this);
222  //création des actions du menu
223  QAction *removeAction = menu.addAction(QObject::tr("&Remove Object"));
224  QAction *addPropertyAction = menu.addAction(QObject::tr("&Add property"));
225  //exécution du menu
226  QAction * actionChoisie=menu.exec(event->screenPos());
227  if(actionChoisie==removeAction)
228  {
229  maman->tableSupprimer(this);
230  }
231  else
232  {
233  if(actionChoisie==addPropertyAction)
234  {
235  maman->tableAjouterChamp(this);
236  }
237  }
238 
239  }
240 
241 
242 QVector<Entite*> Entite::renvoieEntiteMeres()
243 {
244  QVector<Entite*> resultat;
245  foreach (Lien* leLien, vectLiens)
246  {
247  if (QObject::tr("Leak Relation")==leLien->typeDeJointure)
248  {
249  if(leLien->t1==this)
250  {
251  resultat.push_back(leLien->t2);
252  }
253  }
254  }
255  return resultat;
256 }
258 {
259  QStringList resultat;
260  foreach (Lien* leLien, vectLiens)
261  {
262  if (QObject::tr("Cif")==leLien->typeDeJointure or QObject::tr("Df")==leLien->typeDeJointure or QObject::tr("Leak Relation")==leLien->typeDeJointure)
263  {
264  if(leLien->t1==this)
265  {
266  if(leLien->role!=NULL)
267  resultat<<leLien->texteDuRole;
268  else
269  resultat<<leLien->t2->getPrimaryKeyAsFields();
270  }
271  }
272  if (QObject::tr("Leg")==leLien->typeDeJointure)
273  {
274  if(leLien->t2==this)
275  { if(leLien->role!=NULL)
276  resultat<<leLien->texteDuRole;
277  else
278  resultat<<leLien->t1->getPrimaryKeyAsFields();
279  }
280  }
281  }
282  return resultat;
283 }
284 QStringList Entite::renvoieClefsEtrangeres(bool constraintInsideTable)
285 {
286 
287  QStringList resultat;
288  QString debut="";
289  if(!constraintInsideTable)debut="ALTER TABLE "+nomEntite+" ADD ";
290  QString fin="";
291  if(!constraintInsideTable)fin=";";
292 
293  foreach (Lien* leLien, vectLiens)
294  {
295  if (QObject::tr("Cif")==leLien->typeDeJointure or QObject::tr("Df")==leLien->typeDeJointure or QObject::tr("Leak Relation")==leLien->typeDeJointure)
296  {
297  if(leLien->t1==this)
298  {
299  resultat<<debut+leLien->t2->getkeyAsForeignKey(leLien->texteDuRole)+fin;
300  }
301  }
302  if (QObject::tr("Leg")==leLien->typeDeJointure)
303  {
304  if(leLien->t2==this)
305  {
306  resultat<<debut+leLien->t1->getkeyAsForeignKey(leLien->texteDuRole)+fin;
307  }
308  }
309  }
310  return resultat;
311 }
312 bool Entite::canBeWriten(QVector <Entite*>vectEntitesGeneres)
313 {
314  bool resultat=true;
315  foreach (Lien* leLien, vectLiens)
316  {
317  if (QObject::tr("Cif")==leLien->typeDeJointure or QObject::tr("Df")==leLien->typeDeJointure or QObject::tr("Leak Relation")==leLien->typeDeJointure)
318  {
319  if(leLien->t1==this)
320  {
321  resultat=resultat && (vectEntitesGeneres.contains(leLien->t2)||leLien->t1==leLien->t2);
322  }
323  }
324  if (QObject::tr("Leg")==leLien->typeDeJointure)
325  {
326  if(leLien->t2==this)
327  {
328  //resultat<<leLien->t1->getkeyAsForeignKey();
329  resultat=resultat && vectEntitesGeneres.contains(leLien->t1);
330  }
331  }
332  }
333  return resultat;
334 }
335 
337 {
338  return(renvoieEntiteMeres().size()>0);
339 }
340 
342 {
343 
344  if(!association)
345  {
346  if(!isEntiteFaible())//entité
347  {
348  return vecteurChamps[0]->nom;
349  }
350  else//entité faible
351  {
352  //obtention des tables mères
353  QVector<Entite*> entitesMeres=renvoieEntiteMeres();
354  QStringList listeDesNoms;
355  foreach (Entite* lEntite, entitesMeres)
356  {
357  listeDesNoms<<lEntite->getPrimaryKey();
358  }
359  listeDesNoms<<vecteurChamps[0]->nom;
360  return listeDesNoms.join(',');
361  }
362  }
363  else//association
364  {
365  //prendre l'ensemble des clefs de chaque patte de l'assoc
366  QVector<Entite*> vectEnt=((Association*)this)->vectEntitesAssociees();
367  QStringList slPK;
368  foreach (Entite* lEntite, vectEnt) {
369  slPK<<lEntite->getPrimaryKey();
370  }
371  return slPK.join(',');
372  }
373 }
374 
375 
376 QString Entite::getPrimaryKeyAsFields(bool references)
377 {
378  if(!association)
379  {
380  if(!isEntiteFaible())//je suis une entité
381  {
382  QString laChaine=this->vecteurChamps[0]->nom+" "+this->vecteurChamps[0]->sonType;
383  QString taille=this->vecteurChamps[0]->taille;
384  if(!taille.isEmpty()) laChaine+="("+taille+")";
385 
386  return(laChaine);
387  }
388  else//c'est une entite faible
389  {
390  //obtention des tables mères
391  QVector<Entite*> entitesMeres=renvoieEntiteMeres();
392  QStringList listeDesNoms;
393  foreach (Entite* lEntite, entitesMeres)
394  {
395  QString laChaine=lEntite->getPrimaryKeyAsFields();
396  listeDesNoms<<laChaine;
397  }
398  return listeDesNoms.join(',')+','+vecteurChamps[0]->nom;
399  }
400  }
401  else//c'est une association
402  {
403  //prendre l'ensemble des clefs de chaque patte de l'assoc
404  QVector<Entite*> vectEnt=((Association*)this)->vectEntitesAssociees();
405  QStringList slPK;
406  foreach (Entite* lEntite, vectEnt) {
407  slPK<<lEntite->getPrimaryKeyAsFields(false);
408  }
409  return slPK.join(',');
410  }
411 }
412 QString Entite::getkeyAsForeignKey(QString role)
413 {
414 
415  QString motReserve="#ad2bee";
416  QString typeChamp="#87711d";
417  QString nomTable="#29a329";
418  QString nomChamp="#39b339";
419  QString tailleChamp="#97812d";
420  QString laChaine="<font color=\""+motReserve+"\"> foreign key </font> (";
421  if(role.isEmpty())
422  {
423  laChaine+=getPrimaryKey();
424  }
425  else
426  {
427  laChaine+=role;
428  }
429  laChaine+=+") <font color=\""+motReserve+"\">references</font> "+this->nomEntite+"("+getPrimaryKey()+")";
430  return(laChaine);
431 
432 
433 }
434 QString Entite::toSql(bool withoutFK)
435 {
436  qDebug()<<"QString Entite::toSql()";
437  QStringList defs;
438  QString resultat;
439  //le début de la requête
440  QString motReserve="#ad2bee";
441  QString typeChamp="#87711d";
442  QString nomTable="#29a329";
443  QString nomChamp="#39b339";
444  QString tailleChamp="#97812d";
445 
446  resultat="<font color=\""+motReserve+"\">CREATE TABLE</font> <font color=\""+nomTable+"\">`"+nomTableTronque()+"`</font>(";
447  //demander à chaque champ de l'entité de s'écrire ici
448  QStringList defChamp;
449  foreach (Property* leChamp, vecteurChamps) {
450  defs<<leChamp->toSql();
451  }
452 
453  //les champs étrangers
454  ;
455  defs.append(renvoieChampsEtrangers());
456 
457  //les clés étrangères
458 
459  if(!withoutFK) defs.append(renvoieClefsEtrangeres());
460 
461  //la clef primaire
462  defs.append("<font color=\""+motReserve+"\"><b>primary key</b></font>(<u>"+getPrimaryKey()+"</u>)");
463  //ajout des définitions
464  resultat+=defs.join(',');
465  //la toute fin de la requête
466  resultat+=");";
467  return resultat;
468 }
Entite * t2
t2 second object
Definition: lien.h:40
The QCustomGraphicsScene class This class is designed to deal with mouse events.
QString nomEntite
nomEntite name of entity
Definition: entite.h:58
QString toSql(bool withoutFK=false)
toSql
Definition: entite.cpp:434
void selectionne(Entite *lEntite)
selectionne
Definition: mainwindow.cpp:154
void tableAjouterChamp(Entite *lEntite, QString nomDuChamp="property name", QString typeDuChamp="Son type", QString roleDuChamp="Son rôle dans l'objet", QString tailleDuChamp="")
tableAjouterChamp
Definition: mainwindow.cpp:134
void supprimerLien(Lien *leLien)
supprimerLien
Definition: mainwindow.cpp:426
int noLien(Entite *lAutre, Lien *leLien)
noLien
Definition: entite.cpp:96
The Lien class a link is betwwen to objects it contains a line a role text cardinality ...
Definition: lien.h:17
QString toSql()
toSql
Definition: property.cpp:97
Entite * t1
t1 first object
Definition: lien.h:35
QVector< Lien * > vectLiens
vectLiens this vector contains adresses of the links concerning entity
Definition: entite.h:69
QString getPrimaryKey()
getPrimaryKey
Definition: entite.cpp:341
QString nomTableTronque()
nomTableTronque
Definition: entite.h:95
int nbLien(Entite *lAutre)
nbLien
Definition: entite.cpp:109
The Association class Association inherit Entite Association is a relation between at least two entit...
Definition: association.h:9
QString texteDuRole
texteDuRole example: chief
Definition: lien.h:61
Entite(MainWindow *mum, QString nom, qreal x, qreal y, QGraphicsItem *parent, QCustomGraphicsScene *laScene, QStringList listeDesChamps, bool isAssoc)
Entite.
Definition: entite.cpp:120
QStringList renvoieClefsEtrangeres(bool constraintInsideTable=true)
renvoieClefsEtrangeres
Definition: entite.cpp:284
QString typeDeJointure
typeDeJointure cif –1,1–CIF–0,n–> df –0,1–DF–0,n–>or leg --------—
Definition: lien.h:79
QString getPrimaryKeyAsFields(bool=true)
getPrimaryKeyAsFields
Definition: entite.cpp:376
QVector< Property * > vecteurChamps
vecteurChamps container for entity's fields
Definition: entite.h:74
bool isEntiteFaible()
isEntiteFaible
Definition: entite.cpp:336
The Property class property is a field it can be placed into entity or relation.
Definition: field.h:13
void redraw()
redraw
Definition: entite.cpp:52
QStringList renvoieChampsEtrangers()
renvoieChampsEtrangers
Definition: entite.cpp:257
QVector< Entite * > renvoieEntiteMeres()
renvoieEntiteMeres
Definition: entite.cpp:242
bool canBeWriten(QVector< Entite * > vectEntitesGeneres)
canBeWriten
Definition: entite.cpp:312
QGraphicsSimpleTextItem * role
role if there are several links between two same objects
Definition: lien.h:56
void contextMenuEvent(QGraphicsSceneMouseEvent *event)
contextMenuEvent
Definition: entite.cpp:204
void tableSupprimer(Entite *laTableASupprimer, bool demanderConfirmation=true)
tableSupprimer
Definition: mainwindow.cpp:332
QString getkeyAsForeignKey(QString role)
getkeyAsForeignKey
Definition: entite.cpp:412
The Entite class Entity is an object we have to deal with.
Definition: entite.h:20
QGraphicsTextItem * title
title
Definition: entite.h:53
The MainWindow class main code of application.
Definition: mainwindow.h:20
QRectF boundingRect() const
boundingRect
Definition: lien.cpp:393
MainWindow * maman
maman the main window
Definition: entite.h:85
QGraphicsRectItem * rectTitle
rectTitle rectangle wich contains title
Definition: entite.h:49