1 #include "dragdropatomsdlg.h"
2 #include "deprecation.h"
DragDropAtomsDialog(ChGL * gl,Molecule & mole,const QList<int> & reference,QList<int> * target,QWidget * parent)3 DragDropAtomsDialog::DragDropAtomsDialog(ChGL *gl, Molecule &mole, const QList<int> &reference, QList<int> *target, QWidget *parent)
4     :QDialog(parent){
5     chgl=gl;
6     DragDropAtoms *dda = new DragDropAtoms(mole,reference,target,parent);
7     QScrollArea *qsa = new QScrollArea();
8     qsa->setWidget(dda);
9     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
10     ok = buttonBox->button(QDialogButtonBox::Ok);
11 
12     ok->setDisabled(true);
13     connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
14     connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
15     QLabel *usageHint = new QLabel();
16     usageHint->setText("<h1>Inherit labels assigning dialog</h1> <ul><li>If you click on OK here, then atoms on the<br>"
17                        " right column will inherit their Labels from<br> the atoms given in the left column.</li>"
18                        "<li> You can change the ordering of the right<br> column by drag and drop of its items.</li>"
19                        "<li> If you click on the items on the left column<br> the atoms in the structure will<br> be <em>highlighted</em>.</li>"
20                        "<li> You <em>MUST</em> assingn a new <em>ResidueNr</em> and an<br> appropriate <em>Residue Class</em> here!</ul>");
21     usageHint->setStyleSheet (QString(
22                 "QLabel{font: bold; background-color:#ffffaa;}"));
23     QHBoxLayout *la = new QHBoxLayout();
24     setWindowTitle(tr("Inherit labels assigning dialog"));
25     QLabel *resiNrSpinL = new QLabel("Residue Nr:");
26     resiNrSpin = new QSpinBox();
27     resiNrSpin->setMinimum(0);
28     resiNrSpin->setMaximum(9999);
29     resiNrSpin->setValue(0);
30     //connect(resiNrSpin,SIGNAL(valueChanged(int)),this,SLOT(updateLabel()));
31     connect(resiNrSpin,SIGNAL(valueChanged(int)),this,SLOT(renameRNchanged(int)));
32     QLabel *resiResiClassL = new QLabel("Residue Class:");
33     resiResiClass = new QLineEdit("");
34     resiResiClass->setMaxLength(4);
35     resiResiClass->setValidator(new QRegExpValidator(QRegExp("^[A-Za-z]\\w{0,3}"),resiResiClass));
36     resiResiClass->setMinimumWidth(100);
37     connect(resiResiClass,SIGNAL(textEdited(const QString&)),this,SLOT(grossRC(const QString&)));
38     resiResiClass->setEnabled(false);
39     QHBoxLayout *rhla = new QHBoxLayout();
40     rhla->addWidget(resiNrSpinL);
41     rhla->addWidget(resiNrSpin);
42     rhla->addWidget(resiResiClassL);
43     rhla->addWidget(resiResiClass);
44     zoomIn = new QToolButton (this);
45     zoomIn-> setIcon(QIcon(":/xle-icons/zoom-in.png"));
46     zoomIn->setShortcut(QKeySequence(Qt::ALT +Qt::Key_PageUp));
47     zoomIn->setText("Zoom +");
48     zoomIn->setToolTip("Zoom +");
49     zoomIn->setAutoRepeat(true);
50     zoomIn->setAutoRepeatDelay(150);
51     zoomIn->setAutoRepeatInterval(3);
52     zoomOut = new QToolButton(this);
53     zoomOut->setShortcut(QKeySequence(Qt::ALT +Qt::Key_PageDown));
54     zoomOut->setIcon(QIcon(":/xle-icons/zoom-out.png"));
55     zoomOut->setText("Zoom -");
56     zoomOut->setToolTip("Zoom -");
57     zoomOut->setAutoRepeat(true);
58     zoomOut->setAutoRepeatDelay(150);
59     zoomOut->setAutoRepeatInterval(3);
60 
61 
62     rotLeft = new QToolButton(this);
63     rotLeft->setIcon(QIcon(":/xle-icons/moveleft.png"));
64     rotLeft->setText("rotate couter clock wise");
65     rotLeft->setShortcut(QKeySequence(Qt::ALT + Qt::Key_Left));
66     rotLeft->setToolTip("rotate couter clock wise");
67     rotLeft->setAutoRepeat(true);
68     rotLeft->setAutoRepeatDelay(50);
69     rotLeft->setAutoRepeatInterval(3);
70 
71 
72     rotRight = new QToolButton(this);
73     rotRight->setIcon(QIcon(":/xle-icons/moveright.png"));
74     rotRight->setText("rotate clock wise");
75     rotRight->setToolTip("rotate clock wise");
76     rotRight->setShortcut(QKeySequence(Qt::ALT + Qt::Key_Right));
77     rotRight->setAutoRepeat(true);
78     rotRight->setAutoRepeatDelay(50);
79     rotRight->setAutoRepeatInterval(3);
80 
81     rotUp = new QToolButton(this);
82     rotUp->setShortcut(QKeySequence(Qt::ALT + Qt::Key_Up));
83 
84     rotUp->setAutoRepeatDelay(50);
85     rotUp->setAutoRepeat(true);
86     rotUp->setAutoRepeatInterval(3);
87     rotDown = new QToolButton(this);
88     rotDown->setShortcut(QKeySequence(Qt::ALT + Qt::Key_Down));
89     rotDown->setAutoRepeatDelay(50);
90     rotDown->setAutoRepeat(true);
91     rotDown->setAutoRepeatInterval(3);
92 
93     connect(zoomIn,SIGNAL(pressed ()),this,SLOT(zoomin()));
94     connect(zoomOut,SIGNAL(pressed ()),this,SLOT(zoomout()));
95     connect(rotRight,SIGNAL(pressed ()),this,SLOT(rotr()));
96     connect(rotLeft,SIGNAL(pressed ()),this,SLOT(rotl()));
97     connect(rotUp,SIGNAL(pressed ()),this,SLOT(rotu()));
98     connect(rotDown,SIGNAL(pressed ()),this,SLOT(rotd()));
99 
100     QGridLayout *mola = new QGridLayout();
101     mola->addWidget(zoomIn,1,1);
102     mola->addWidget(zoomOut,0,0);
103     mola->addWidget(rotLeft,1,0);
104     mola->addWidget(rotRight,1,2);
105     mola->addWidget(rotUp,0,1);
106     mola->addWidget(rotDown,2,1);
107     QPixmap up = QPixmap(":/xle-icons/moveright.png");
108     QTransform transform;
109     QTransform t90 = transform.rotate(-90);
110     QTransform t270 = transform.rotate(180);
111     rotUp->setIcon(up.transformed(t90));
112     rotDown->setIcon(up.transformed(t270));
113     QVBoxLayout *lo = new QVBoxLayout();
114     la->addWidget(qsa);
115     la->addWidget(usageHint);
116     lo->addLayout(la);
117     lo->addLayout(rhla);
118     rhla->addLayout(mola);
119     lo->addWidget(buttonBox);
120     setLayout(lo);
121     setMinimumWidth(190+usageHint->sizeHint().width());
122     connect(dda,SIGNAL(pairs(QString)),this,SIGNAL(pairs(QString)));
123 }
124 
grossRC(const QString & text)125 void DragDropAtomsDialog::grossRC(const QString &text){
126   QString s=text.toUpper ();
127   resiResiClass->setText(s);
128   update();
129 }
130 
renameRNchanged(int ii)131 void DragDropAtomsDialog::renameRNchanged(int ii){
132   QMap<int,QString> resNrClass;
133   for (int i=0; i<resiFinder->count(); i++){
134     QString resil=resiFinder->itemText(i).toUpper();
135     resil.remove(QRegExp("^RESI"));
136     QString resinr=resil;
137     resil.remove(QRegExp("\\b\\d+\\b"));
138     resinr.remove(resil.trimmed());
139     int rn=resinr.trimmed().toInt();
140     resNrClass[rn]=resil.trimmed();
141   }
142   if (!ii) {
143     resiResiClass->setEnabled(false);
144     ok->setDisabled(true);
145     resiResiClass->setText("");
146   }
147   else {
148     resiResiClass->setDisabled(resNrClass.contains(ii));
149     ok->setDisabled(resNrClass.contains(ii));
150     resiResiClass->setText(resNrClass[ii]);
151   }
152 
153 }
154 
zoomin()155 void DragDropAtomsDialog::zoomin(){
156   chgl->zoom(0.1);
157   chgl->updateGL();
158 }
159 
zoomout()160 void DragDropAtomsDialog::zoomout(){
161   chgl->zoom(-0.1);
162   chgl->updateGL();
163 }
164 
rotr()165 void DragDropAtomsDialog::rotr(){
166   chgl->rotY(-0.05);
167   chgl->updateGL();
168 }
169 
rotl()170 void DragDropAtomsDialog::rotl(){
171   chgl->rotY(0.05);
172   chgl->updateGL();
173 }
174 
rotu()175 void DragDropAtomsDialog::rotu(){
176   chgl->rotX(0.05);
177   chgl->updateGL();
178 }
179 
rotd()180 void DragDropAtomsDialog::rotd(){
181   chgl->rotX(-0.05);
182   chgl->updateGL();
183 }
184 
DragDropAtoms(Molecule & mole,const QList<int> & reference,QList<int> * target,QWidget * parent)185 DragDropAtoms::DragDropAtoms(Molecule &mole, const QList<int> &reference, QList<int> *target, QWidget *parent)
186     :QWidget(parent){
187   m_mole = mole;
188   m_reference = reference;
189   m_target = target;
190 
191 //  setMouseTracking(true);
192   int x = 5;
193   int y = 5;
194   dadas.clear();
195   //printf("%d %d \n",reference.size(),target.size());
196   for (int i=0; i<target->size(); i++){
197     int j=target->at(i);
198     int k=reference.at(i);
199     dadas.append(QString("%1 %2").arg(k).arg(j));
200     //printf("%d %d %s\n",i,j,mole.asymm.at(j).Label.toStdString().c_str());
201     QLabel * l =new QLabel(mole.asymm.at(k).Label,this);
202     l->move(x, y);
203     l->show();
204     l->setAcceptDrops(false);
205     l->setAlignment(Qt::AlignCenter);
206     l->setAttribute(Qt::WA_DeleteOnClose);
207     l->setStyleSheet (QString(
208             "QLabel{font: bold; border: 2px solid #2f2f31; border-radius: 1px;background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 %1, stop: 1 #dadbde);} "
209             "QLabel:hover {background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0,"
210                         "stop: 0 #ffffff, stop: 1 %1);border: 0px;}").arg(mole.AtomColor[mole.asymm.at(k).an].name()));
211     DragAtom *wordLabel = new DragAtom(j,mole.asymm.at(j).Label,mole.AtomColor[mole.asymm.at(j).an], this);
212     wordLabel->move(60, y);
213     l->setFixedSize(47,wordLabel->height()-2);
214     wordLabel->show();
215     wordLabel->setFixedWidth(56);
216     wordLabel->setAttribute(Qt::WA_DeleteOnClose);
217     dragAtoms.append(wordLabel);
218     itemheight=wordLabel->height();
219     y += wordLabel->height() + 2;
220   }
221 
222   QPalette newPalette = palette();
223   newPalette.setColor(QPalette::Window, Qt::white);
224   setPalette(newPalette);
225 
226   setFixedSize(120, qMax(200, y));
227   setAcceptDrops(true);
228 }
229 
dragEnterEvent(QDragEnterEvent * event)230 void DragDropAtoms::dragEnterEvent(QDragEnterEvent *event){
231 
232   if (event->mimeData()->hasFormat("application/x-dragdropatom")) {
233     //printf("dragEnterEvent\n");
234     if (children().contains(event->source())) {
235       event->setDropAction(Qt::MoveAction);
236       event->accept();
237     } else {
238       event->acceptProposedAction();
239     }
240   }  else {
241     event->ignore();
242   }
243 }
244 
dragMoveEvent(QDragMoveEvent * event)245 void DragDropAtoms::dragMoveEvent(QDragMoveEvent *event){
246 
247   if (event->mimeData()->hasFormat("application/x-dragdropatom")) {
248     //printf("dragMoveEvent\n");
249     int idx =  (event->pos().y()-5) / (itemheight+2);
250     //printf("idx %d  current %d\n",idx,current);
251     for (int i = 0; i< dragAtoms.size(); i++){
252         int k=0;
253         if ((idx<current)&&(i>=idx)&&(i<current)) k=1;
254         if ((idx>current)&&(i<=idx)&&(i>=current)) k=-1;
255         dragAtoms.at(i)->move(60, 5+(itemheight+2)*(i+k));
256     }
257     if (children().contains(event->source())) {
258       event->setDropAction(Qt::MoveAction);
259 
260       event->accept();
261     } else {
262       event->acceptProposedAction();
263     }
264   } else {
265     event->ignore();
266   }
267   update();
268 }
269 
dropEvent(QDropEvent * event)270 void DragDropAtoms::dropEvent(QDropEvent *event){
271 
272   if (event->mimeData()->hasFormat("application/x-dragdropatom")) {
273     const QMimeData *mime = event->mimeData();
274     QByteArray itemData = mime->data("application/x-dragdropatom");
275     QDataStream dataStream(&itemData, QIODevice::ReadOnly);
276     int idx;
277     QColor cc;
278     QString text;
279     QPoint offset;
280     dataStream >>idx>>cc>> text >> offset;
281     int index =  (event->pos().y()-5) / (itemheight+2);
282 
283     m_target->removeOne(idx);
284     m_target->insert(index,idx);
285     //text=text.toLower();
286     DragAtom *newLabel = new DragAtom(idx,text,cc, this);
287     newLabel->move(event->pos() - offset);
288     newLabel->show();
289     newLabel->setAttribute(Qt::WA_DeleteOnClose);
290     dragAtoms.removeAt(current);
291     dragAtoms.insert(index,newLabel);
292     //printf("dropEvent\n");
293     if (event->source() == this) {
294       event->setDropAction(Qt::MoveAction);
295       event->accept();
296     } else {
297       event->acceptProposedAction();
298     }
299     dadas.clear();
300     for (int i = 0; i< dragAtoms.size(); i++){
301         dragAtoms.at(i)->move(60, 5+(itemheight+2)*(i));
302         int j=m_target->at(i);
303 
304         int k=m_reference.at(i);
305 
306         dadas.append(QString("%1 %2").arg(k).arg(j));
307         printf("%d %s %s\n",i,m_mole.asymm.at(k).Label.toStdString().c_str(),m_mole.asymm.at(j).Label.toStdString().c_str());
308     }
309   } /*else if (event->mimeData()->hasText()) {
310     QStringList pieces = event->mimeData()->text().split(QRegExp("\\s+"),
311 		    skipEmptyParts);
312     QPoint position = event->pos();
313 
314     foreach (QString piece, pieces) {
315       DragAtom *newLabel = new DragAtom(0,piece,Qt::white this);
316       newLabel->move(position);
317       newLabel->show();
318       newLabel->setAttribute(Qt::WA_DeleteOnClose);
319 
320       position += QPoint(newLabel->width(), 0);
321     }
322 
323     event->acceptProposedAction();
324   }*/ else {
325     event->ignore();
326   }
327 }
328 /*
329 void DragDropAtoms::mouseMoveEvent(QMouseEvent *event){
330     if (event->buttons()==Qt::NoButton){
331         static int last=-1;
332         int index =  (event->pos().y()-5) / (itemheight+2);
333         printf("index %d x=%d y=%d  %d\n",index,event->pos().x(),event->pos().y(),itemheight);
334         QString dada="";
335 
336 
337         if ((index<0)||(index>=m_reference.size())) {
338             emit pairs(dada);
339             last=index;
340             return;
341         }
342         if ((event->pos().x()>5)){
343             emit pairs(dadas.at(index));
344 
345             last=index;
346             return;
347         }
348 
349         last=index;
350        emit pairs(dada);
351     }
352 }
353 */
mousePressEvent(QMouseEvent * event)354 void DragDropAtoms::mousePressEvent(QMouseEvent *event){
355     if (event->pos().x()<50) {
356 
357         int index =  (event->pos().y()) / (itemheight+2);
358         emit pairs(dadas.at(index));
359         event->accept();return;}
360   DragAtom *child = static_cast<DragAtom*>(childAt(event->pos()));
361   if (!child)
362     return;
363 
364   QPoint hotSpot = event->pos() - child->pos();
365 
366   QByteArray itemData;
367   QDataStream dataStream(&itemData, QIODevice::WriteOnly);
368   dataStream<<child->index()<<child->color() << child->labelText() << QPoint(hotSpot);
369   current = m_target->indexOf(child->index());
370   QMimeData *mimeData = new QMimeData;
371   mimeData->setData("application/x-dragdropatom", itemData);
372   mimeData->setText(child->labelText());
373 
374   QDrag *drag = new QDrag(this);
375   drag->setMimeData(mimeData);
376   drag->setPixmap(*child->pixmap());
377   drag->setHotSpot(hotSpot);
378 
379   child->hide();
380 
381   if (drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction) == Qt::MoveAction)
382     child->close();
383   else
384     child->show();
385 }
386 //void DragDropAtoms::
387 //void DragDropAtoms::
388 //
389 //
DragAtom(const int index,const QString & text,QColor c,QWidget * parent)390 DragAtom::DragAtom(const int index,const QString &text,QColor c, QWidget *parent): QLabel(parent){
391   m_index=index;
392   m_labelText=text;
393   m_c=c;
394   QFontMetrics metric(font());
395   QSize size = metric.size(Qt::TextSingleLine, " "+ text+" ");
396 
397   QImage image(size.width() + 14, size.height() + 14,
398 		  QImage::Format_ARGB32_Premultiplied);
399   image.fill(qRgba(0, 0, 0, 0));
400 
401   QFont font;
402   font.setStyleStrategy(QFont::ForceOutline);
403   font.setWeight(QFont::Black);
404 
405   QLinearGradient gradient(0, 0, 0, image.height()-1);
406   gradient.setColorAt(0.0, Qt::white);
407   gradient.setColorAt(0.2, c);
408   gradient.setColorAt(0.8, c);
409   gradient.setColorAt(1.0, QColor(127, 127, 200));
410 
411   QPainter painter;
412   painter.begin(&image);
413   painter.setRenderHint(QPainter::Antialiasing);
414   painter.setBrush(gradient);
415   painter.drawRoundedRect(QRectF(0.5, 0.5, image.width()-1, image.height()-1),
416 		  25, 25, Qt::RelativeSize);
417 
418   painter.setFont(font);
419   painter.setBrush(Qt::black);
420   painter.drawText(QRect(QPoint(6, 6), size), Qt::AlignCenter, text);
421   painter.end();
422 
423   setPixmap(QPixmap::fromImage(image));
424 }
425 
labelText() const426 QString DragAtom::labelText() const {
427   return m_labelText;
428 }
429 
index() const430 int DragAtom::index() const{
431   return m_index;
432 }
433 
color() const434 QColor DragAtom::color() const{
435 return m_c;
436 }
437