File: TreeView.cpp

package info (click to toggle)
juffed 0.10-89-g3690b60-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 4,132 kB
  • sloc: cpp: 21,060; ansic: 446; xml: 329; sh: 68; makefile: 20
file content (105 lines) | stat: -rw-r--r-- 2,596 bytes parent folder | download | duplicates (5)
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
#include <QDebug>

#include "TreeView.h"

#include <QInputDialog>
#include <QMessageBox>
#include <QKeyEvent>
#include <QHeaderView>
#include <QMenu>
#include <QFileSystemModel>

#include <PluginSettings.h>

TreeView::TreeView(JuffPlugin* plugin, QWidget* parent) 
: QTreeView(parent)
, plugin_(plugin)
, headerMenu_(NULL)
{
	header()->installEventFilter(this);
}

void TreeView::initMenu() {
	headerMenu_ = new QMenu(this);
	int hCount = header()->count();
	for ( int i = 1; i < hCount; ++i ) {
		QString title = model()->headerData(i, Qt::Horizontal).toString();
		QAction* a = headerMenu_->addAction(title, this, SLOT(showHideColumn()));
		a->setData(i);
		a->setCheckable(true);
		bool visible = PluginSettings::getBool(plugin_, QString("column%1").arg(i));
		if ( visible )
			a->setChecked(true);
		else
			setColumnHidden(i, true);
	}
}

void TreeView::keyPressEvent(QKeyEvent* e) {
	if ( e->modifiers() == Qt::NoModifier ) {
		int key = e->key();
		switch ( key ) {
			case Qt::Key_Backspace :
				emit goUp();
				break;
			
			case Qt::Key_Return :
			case Qt::Key_Enter :
				emit doubleClicked(currentIndex());
				setFocus();
				break;
			
			case Qt::Key_F2 :
				renameCurrent();
				break;
			
			default: ;
		}
	}
	else if ( e->modifiers() == Qt::AltModifier ) {
		if ( e->key() == Qt::Key_Up ) {
			emit goUp();
			return;
		}
	}
	QTreeView::keyPressEvent(e);
}

void TreeView::renameCurrent() {
    QFileSystemModel* dirModel = qobject_cast<QFileSystemModel*>(model());
	if ( 0 != dirModel ) {
		QFileInfo fi = dirModel->fileInfo(currentIndex());
		QString newFileName = QInputDialog::getText(this, tr("Rename"), tr("File name"), 
		                                            QLineEdit::Normal, fi.fileName());
        if (newFileName.isEmpty())
            return;

        QFile file(fi.absoluteFilePath());
        QDir::setCurrent(fi.absolutePath());
        if ( !file.rename(newFileName) ) {
            QMessageBox::warning(this, tr("Warning"),
                                 tr("Rename failed: file '%1' already exists").arg(newFileName));
        }
	}
}

bool TreeView::eventFilter(QObject *obj, QEvent *event) {
	if ( obj == header() && event->type() == QEvent::ContextMenu ) {
		headerMenu_->exec(QCursor::pos());
		return true;
	}
	return false;
}

void TreeView::showHideColumn() {
	QAction* a = qobject_cast<QAction*>(sender());
	if ( 0 != a ) {
		int col = a->data().toInt();
		if ( col >= 0 ) {
			bool curHidden = isColumnHidden(col);
			setColumnHidden(col, !curHidden);
			PluginSettings::set(plugin_, QString("column%1").arg(col), curHidden);
		}
	}
}