aboutsummaryrefslogtreecommitdiff
path: root/desktop-utilities/lumina-terminal/TermWindow.cpp
blob: 6dfab80ae3351e924c0eb45adb270c848d1e26bd (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//===========================================
//  Lumina-DE source code
//  Copyright (c) 2015, Ken Moore
//  Available under the 3-clause BSD license
//  See the LICENSE file for full details
//===========================================
#include "TermWindow.h"
//#include "ui_TermWindow.h"

#include <QDesktopWidget>
#include <QDebug>
#include <QTimer>
#include <QApplication>
#include <QVBoxLayout>
#include "TerminalWidget.h"

// ===============
//        PUBLIC
// ===============
TermWindow::TermWindow(QSettings *set) : QWidget(0, Qt::Window | Qt::BypassWindowManagerHint){//, ui(new Ui::TermWindow){
  CLOSING = false; //internal flag
  settings = set;
  //Create the Window
  this->setLayout(new QVBoxLayout());
  this->setCursor(Qt::SplitVCursor);
  tabWidget = new QTabWidget(this);
    tabWidget->clear(); //just in case
    tabWidget->setCursor(Qt::ArrowCursor);
    tabWidget->setTabBarAutoHide(true);
    tabWidget->setTabsClosable(true);
    tabWidget->setMovable(true);
    tabWidget->setUsesScrollButtons(true);
    this->layout()->addWidget(tabWidget);
  //Setup the animation
  ANIM = new QPropertyAnimation(this, "geometry", this);
    ANIM->setDuration(300); //1/3 second animation
  connect(ANIM, SIGNAL(finished()), this, SLOT(AnimFinished()) );
  //Create the keyboard shortcuts
  hideS = new QShortcut(QKeySequence(Qt::Key_Escape),this);
  closeS = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_Q),this);
  newTabS = new QShortcut(QKeySequence::AddTab,this);
  closeTabS = new QShortcut(QKeySequence::Close,this);
  //Connect the signals/slots
  connect(tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(Close_Tab(int)) );
  connect(closeTabS, SIGNAL(activated()), this, SLOT(Close_Tab()) );
  connect(newTabS, SIGNAL(activated()), this, SLOT(New_Tab()) );
  connect(hideS, SIGNAL(activated()), this, SLOT(HideWindow()) );
  connect(closeS, SIGNAL(activated()), this, SLOT(CloseWindow()) );
  
  //Now set the defaults
  screennum = 0; //default value
  setTopOfScreen(true); //default value
  if(settings->contains("lastSize")){ 
    //qDebug() << "Re-use last size:" << settings->value("lastSize").toSize();
    this->resize( settings->value("lastSize").toSize() ); 
    CalculateGeom();
    //qDebug() << "After size:" << this->size();
  }
  
  //this->resize(this->width(),300);
  //this->setMinimumSize(20, 300);
  
}


TermWindow::~TermWindow(){
	
}

void TermWindow::cleanup(){
  //called right before the window is closed
  //Make sure to close any open tabs/processes
  CLOSING = true;
  for(int i=0; i<tabWidget->count(); i++){
    static_cast<TerminalWidget*>(tabWidget->widget(i))->aboutToClose();
  }
}

void TermWindow::OpenDirs(QStringList dirs){
  for(int i=0; i<dirs.length(); i++){
    //Open a new tab for each directory
    TerminalWidget *page = new TerminalWidget(tabWidget, dirs[i]);
    QString ID = GenerateTabID();
      page->setWhatsThis(ID);
    tabWidget->addTab(page, ID);
    qDebug() << "New Tab:" << ID << dirs[i];
    connect(page, SIGNAL(ProcessClosed(QString)), this, SLOT(Close_Tab(QString)) );
  }
}

void TermWindow::setCurrentScreen(int num){
    screennum = num;
    QTimer::singleShot(0,this, SLOT(ReShowWindow()));
}

void TermWindow::setTopOfScreen(bool ontop){
    onTop = ontop;
    this->layout()->setContentsMargins(0, (onTop ? 0 : 3), 0, (onTop ? 3 : 0));
    tabWidget->setTabPosition(onTop ? QTabWidget::South : QTabWidget::North);
    QTimer::singleShot(0,this, SLOT(ReShowWindow()));
}

// =======================
//       PUBLIC SLOTS
// =======================
void TermWindow::ShowWindow(){
  if(animRunning>=0){ return; } //something running
  animRunning = 1;
  this->hide();
  QApplication::processEvents();
  CalculateGeom();
  //Now setup the animation
  ANIM->setEndValue(this->geometry());
  if(onTop){ //use top edge
    ANIM->setStartValue( QRect(this->x(), this->y(), this->width(), 0) ); //same location - no height
  }else{
    ANIM->setStartValue( QRect(this->x(), this->geometry().bottom(), this->width(), 0) ); //same location - no height
  }
  this->show();
  qDebug() << "Start Animation" << ANIM->startValue() << ANIM->endValue();
  ANIM->start();
}

void TermWindow::HideWindow(){
  if(animRunning>=0){ return; } //something running
  //Now setup the animation
  //Note: Do *not* use the private settings/variables because it may be changing right now - use the current geometry *ONLY*
  animRunning = 0;
  ANIM->setStartValue(this->geometry());
  QDesktopWidget *desk = QApplication::desktop();
  int screen = desk->screenNumber(this); //which screen it is currently on
  if(desk->availableGeometry(screen).top() == this->geometry().top()){ //use top edge
    ANIM->setEndValue( QRect(this->x(), this->y(), this->width(), 0) ); //same location - no height
  }else{
    ANIM->setEndValue( QRect(this->x(), this->y()+this->height(), this->width(), 0) ); //same location - no height
  }
  this->show();
  ANIM->start();	
}

void TermWindow::CloseWindow(){
  if(animRunning>=0){ return; } //something running
  //Now setup the animation
  animRunning = 2;
  ANIM->setStartValue(this->geometry());
  if(onTop){ //use top edge
    ANIM->setEndValue( QRect(this->x(), this->y(), this->width(), 0) ); //same location - no height
  }else{
    ANIM->setEndValue( QRect(this->x(), this->geometry().bottom(), this->width(), 0) ); //same location - no height
  }
  this->show();
  ANIM->start();		
}

void TermWindow::ReShowWindow(){
  if(this->isVisible()){
    HideWindow(); //start with same animation as hide
    animRunning = 3; //flag as a re-show (hide, then show);
  }else{
    //Already hidden, just show it
    ShowWindow();
  }
}
// =======================
//             PRIVATE
// =======================
void TermWindow::CalculateGeom(){
  //qDebug() << "Calculating Geom:" << this->size();
  QDesktopWidget *desk = QApplication::desktop();
  if(desk->screenCount() <= screennum){ screennum = desk->primaryScreen(); } //invalid screen detected 
  //Now align the window with the proper screen edge
  QRect workarea = desk->availableGeometry(screennum); //this respects the WORKAREA property
  if(onTop){
    this->setGeometry( workarea.x(), workarea.y(), workarea.width(), this->height()); //maintain current hight of window
	  
  }else{
    this->setGeometry( workarea.x(), workarea.y() + workarea.height() - this->height(), workarea.width(), this->height()); //maintain current hight of window
  }
  this->setFixedWidth(this->width()); //Make sure the window is not re-sizeable in the width dimension
  this->setMinimumHeight(0);
}

QString TermWindow::GenerateTabID(){
  //generate a unique ID for this new tab
  int num = 1;
  for(int i=0; i<tabWidget->count(); i++){
    if(tabWidget->widget(i)->whatsThis().toInt() >= num){ num = tabWidget->widget(i)->whatsThis().toInt()+1; }
  }
  return QString::number(num);
}

// =======================
//        PRIVATE  SLOTS
// =======================

//Tab Interactions
void TermWindow::New_Tab(){
  OpenDirs(QStringList() << QDir::homePath());
}

void TermWindow::Close_Tab(int tab){
  //qDebug() << "Close Tab:" << tab;
  if(tab<0){ tab = tabWidget->currentIndex(); }
  static_cast<TerminalWidget*>(tabWidget->widget(tab))->aboutToClose();
  delete tabWidget->widget(tab); //delete the page within the tag
  tabWidget->removeTab(tab); // remove the tab itself
  //Make sure there is always at least one tab
  if(tabWidget->count() < 1){ 
    if(CLOSING){ emit TerminalFinished(); }
    else{ New_Tab();  }
  }
}

void TermWindow::Close_Tab(QString ID){
  //Close a tab based on it's ID instead of it's tab number
  for(int i=0; i<tabWidget->count(); i++){
    if(tabWidget->widget(i)->whatsThis()==ID){
      Close_Tab(i);
      return; //all done
    }
  }
}

//Animation finishing
void TermWindow::AnimFinished(){
  if(animRunning <0){ return; } //nothing running
  if(animRunning==0){
    //Hide Event
    this->hide(); //need to hide the whole thing now
    this->setGeometry( ANIM->startValue().toRect() ); //reset back to initial size after hidden
    emit TerminalHidden();
  }else if(animRunning==1){
    //Show Event
    this->activateWindow();
    tabWidget->currentWidget()->setFocus();
    emit TerminalVisible();
  }else if(animRunning==2){
    //Close Event
    this->hide(); //need to hide the whole thing now  
    emit TerminalClosed();
  }else if(animRunning>2){
    //Re-Show event
    this->hide();
    this->setGeometry( ANIM->startValue().toRect() ); //reset back to initial size after hidden
    //Now re-show it
    QTimer::singleShot(0,this, SLOT(ShowWindow()));
  }
  animRunning = -1; //done
}
 
// ===================
//        PROTECTED
// ===================
void TermWindow::mouseMoveEvent(QMouseEvent *ev){
  //Note: With mouse tracking turned off, this event only happens when the user is holding down the mouse button
    if(onTop){
      //Move the bottom edge to the current point
      if( (ev->globalPos().y() - this->y()) < 50){ return; } //quick check that the window is not smaller than 20 pixels
      QRect geom = this->geometry();
	    geom.setBottom(ev->globalPos().y());
      this->setGeometry(geom);
    }else{
      //Move the top edge to the current point
      if( (this->y() + this->height() -ev->globalPos().y()) < 50){ return; } //quick check that the window is not smaller than 20 pixels
      QRect geom = this->geometry();
	    geom.setTop(ev->globalPos().y());
      this->setGeometry(geom);
    }
    settings->setValue("lastSize",this->geometry().size());
}
bgstack15