aboutsummaryrefslogtreecommitdiff
path: root/src-qt5/core/libLumina/ResizeMenu.cpp
blob: cf5b124d76caf4777d84317eb55c37e7b1be3308 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//===========================================
//  Lumina-DE source code
//  Copyright (c) 2013-2016, Ken Moore
//  Available under the 3-clause BSD license
//  See the LICENSE file for full details
//===========================================
#include "ResizeMenu.h"
#include <QDebug>

// =======================
//      RESIZEMENU CLASS
// =======================
ResizeMenu::ResizeMenu(QWidget *parent) : QMenu(parent){
  this->setContentsMargins(1,1,1,1);
  this->setMouseTracking(true);
  resizeSide = NONE;
  cAct = new QWidgetAction(this);
  contents = 0;
  connect(this, SIGNAL(aboutToShow()), this, SLOT(clearFlags()) );
  connect(this, SIGNAL(aboutToHide()), this, SLOT(clearFlags()) );
  connect(cAct, SIGNAL(hovered()), this, SLOT(clearFlags()) );
}

ResizeMenu::~ResizeMenu(){

}

void ResizeMenu::setContents(QWidget *con){
  this->clear();
  cAct->setDefaultWidget(con);
  this->addAction(cAct);
  contents = con; //save for later
  contents->setCursor(Qt::ArrowCursor);
  resyncSize();
}

void ResizeMenu::resyncSize(){
  if(contents==0){ return; }
  qDebug() << "Resync Size:" << this->size() << contents->size();
  this->resize(contents->size());
  qDebug() << " - after menu resize:" << this->size() << contents->size();
  emit MenuResized(this->size());
}

void ResizeMenu::mouseMoveEvent(QMouseEvent *ev){
  QRect geom = this->geometry();
  //Note: The exact position does not matter as much as the size
  //  since the window will be moved again the next time it is shown
  // The "-2" in the sizing below accounts for the menu margins
  QPoint gpos = this->mapToGlobal(ev->pos());
  bool handled = false;
  switch(resizeSide){
    case TOP:
	if(gpos.y() >= geom.bottom()-1){ break; }
	geom.setTop(gpos.y());
        this->setGeometry(geom);
        if(contents!=0){ contents->setFixedSize(QSize(geom.width()-2, geom.height()-2));}
	handled = true;
        break;
    case BOTTOM:
	if(gpos.y() <= geom.top()+1){ break; }
	geom.setBottom( gpos.y());
        this->setGeometry(geom);
        if(contents!=0){ contents->setFixedSize(QSize(geom.width()-2, geom.height()-2));}
	handled = true;
        break;
    case LEFT:
	if(gpos.x() >= geom.right()-1){ break; }
	geom.setLeft(gpos.x());
        this->setGeometry(geom);
        if(contents!=0){ contents->setFixedSize(QSize(geom.width()-2, geom.height()-2));}
	handled = true;
        break;
    case RIGHT:
	if(gpos.x() <= geom.left()+1){ break; }
	geom.setRight(gpos.x());
        this->setGeometry(geom);
        if(contents!=0){ contents->setFixedSize(QSize(geom.width()-2, geom.height()-2));}
	handled = true;
        break;
    default: //NONE
	//qDebug() << " - Mouse At:" << ev->pos();
	//Just adjust the mouse cursor which is shown
        if(ev->pos().x()<=1 && ev->pos().x() >= -1){ this->setCursor(Qt::SizeHorCursor); }
        else if(ev->pos().x() >= this->width()-1 && ev->pos().x() <= this->width()+1){  this->setCursor(Qt::SizeHorCursor); }
        else if(ev->pos().y()<=1 && ev->pos().y() >= -1){ this->setCursor(Qt::SizeVerCursor); }
        else if(ev->pos().y() >= this->height()-1 && ev->pos().y() <= this->height()+1){ this->setCursor(Qt::SizeVerCursor); }
	else{ this->setCursor(Qt::ArrowCursor); }
  }
  if(!handled){ QMenu::mouseMoveEvent(ev); }  //do normal processing as well
}

void ResizeMenu::mousePressEvent(QMouseEvent *ev){
  bool used = false;
  if(ev->buttons().testFlag(Qt::LeftButton) && resizeSide==NONE){
    //qDebug() << "Mouse Press Event:" <<  ev->pos() << resizeSide;
    if(ev->pos().x()<=1 && ev->pos().x() >= -1){resizeSide = LEFT; used = true;}
    else if(ev->pos().x() >= this->width()-1 && ev->pos().x() <= this->width()+1){ resizeSide = RIGHT; used = true;}
    else if(ev->pos().y()<=1 && ev->pos().y() >= -1){ resizeSide = TOP; used = true; }
    else if(ev->pos().y() >= this->height()-1 && ev->pos().y() <= this->height()+1){ resizeSide = BOTTOM; used = true; }
  }
  if(used){ ev->accept(); this->grabMouse(); }
  else{ QMenu::mousePressEvent(ev); } //do normal processing
}

void ResizeMenu::mouseReleaseEvent(QMouseEvent *ev){
  this->releaseMouse();
  if(ev->button() == Qt::LeftButton && resizeSide!=NONE ){
    //qDebug() << "Mouse Release Event:" <<  ev->pos() << resizeSide;
    resizeSide = NONE;
    emit MenuResized(contents->size());
    ev->accept();
  }else{
    QMenu::mouseReleaseEvent(ev);  //do normal processing
  }
}
bgstack15