aboutsummaryrefslogtreecommitdiff
path: root/src-qt5/desktop-utils/lumina-terminal/TermWindow.cpp
blob: 812d36798fbe9365e1dd64d04699ab438362dd21 (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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//===========================================
//  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){
  this->setWindowOpacity(0.85);
  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);
    tabWidget->setFocusPolicy(Qt::ClickFocus);
    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()) );
  activeTimer = new QTimer(this);
    activeTimer->setInterval(50);
    activeTimer->setSingleShot(true);
    connect(activeTimer, SIGNAL(timeout()), this, SLOT(activeStatusChanged()) );
    connect(QApplication::instance(), SIGNAL(applicationStateChanged(Qt::ApplicationState)), activeTimer, SLOT(start()) );
  //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);
  prevTabS = new QShortcut(QKeySequence::PreviousChild,this);
  nextTabS = new QShortcut(QKeySequence::NextChild,this);
  //Print out all the keyboard shortcuts onto the screen
  qDebug() << "New Tab Shortcut:" << QKeySequence::keyBindings(QKeySequence::AddTab);
  qDebug() << "Close Tab Shortcut:" << QKeySequence::keyBindings(QKeySequence::Close);
  qDebug() << "Next Tab Shortcut:" << QKeySequence::keyBindings(QKeySequence::NextChild);
  qDebug() << "Previous Tab Shortcut:" << QKeySequence::keyBindings(QKeySequence::PreviousChild);
  //Connect the signals/slots
  connect(tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(Close_Tab(int)) );
  connect(tabWidget, SIGNAL(currentChanged(int)), this, SLOT(focusOnWidget()) );
  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()) );
  connect(prevTabS, SIGNAL(activated()), this, SLOT(Prev_Tab()) );
  connect(nextTabS, SIGNAL(activated()), this, SLOT(Next_Tab()) );
  //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);
    tabWidget->setCurrentWidget(page);
    QTimer::singleShot(500, this, SLOT(focusOnWidget()));//page->setFocus();
    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();
  tabWidget->widget(tab)->deleteLater(); //delete the page within the tag
  tabWidget->removeTab(tab); // remove the tab itself
  //Let the tray know when the last terminal is closed
  if(tabWidget->count() < 1){ 
    emit TerminalFinished();
  }
}

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

void TermWindow::Next_Tab(){
  qDebug() << "Next Tab";
  int next = tabWidget->currentIndex()+1;
  if(next>=tabWidget->count()){ next = 0; }
  tabWidget->setCurrentIndex(next);
}

void TermWindow::Prev_Tab(){
  qDebug() << "Previous Tab";
  int next = tabWidget->currentIndex()-1;
  if(next<0){ next = tabWidget->count()-1; }
  tabWidget->setCurrentIndex(next);	
}

void TermWindow::focusOnWidget(){
  if(tabWidget->currentWidget()!=0){
    //qDebug() << "Focus on Widget";
    tabWidget->currentWidget()->setFocus();
  }
}

//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
}
 
void TermWindow::activeStatusChanged(){
  if(animRunning>=0){ return; } //ignore this event - already changing
  QWidget *active = QApplication::activeWindow();
  if(active==0 && this->isVisible()){ HideWindow(); }
}

// ===================
//        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