TreeView.cpp
No OneTemporary

File Metadata

Created
Tue, Jul 7, 9:39 PM

TreeView.cpp

#include <QDebug>
#include "TreeView.h"
#include <QInputDialog>
#include <QMessageBox>
#include <QKeyEvent>
#include <QHeaderView>
#include <QMenu>
#include <QFileSystemModel>
#include <PluginSettings.h>
#include <QFileSystemModel>
TreeView::TreeView(JuffPlugin* plugin, QFileSystemModel* model, QWidget* parent)
: QTreeView(parent)
, plugin_(plugin)
, __model(model)
{
header()->installEventFilter(this);
}
void TreeView::initMenu() {
__menuSection = new QMenu(this);
__menuTopic = new QMenu(this);
__actionRename = __menuSection->addAction("Rename", this, SLOT(rename()));
__menuSection->addAction("Add section", this, SLOT(addChildSection()));
__menuSection->addAction("Add topic", this, SLOT(addChildTopic()));
__menuTopic->addAction("Rename", this, SLOT(rename()));
}
void TreeView::mousePressEvent(QMouseEvent * e){
__actionRename->setDisabled(false);
if (e->button() == Qt::RightButton){
QModelIndex currentIndex = this->indexAt(e->pos());
if (currentIndex.isValid()){
setCurrentIndex(currentIndex);
QString path = __model->filePath(currentIndex);
if ( QFileInfo(path).isDir() ) {
__menuSection->exec(QCursor::pos());
}
else {
__menuTopic->exec(QCursor::pos());
}
return;
}
setCurrentIndex(rootIndex());
__actionRename->setDisabled(true);
__menuSection->exec(QCursor::pos());
}
QTreeView::mousePressEvent(e);
}
void TreeView::rename() {
this->edit(currentIndex());
// 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.baseName());
// if (newFileName.isEmpty())
// return;
// QString oldFileName = fi.absoluteFilePath();
// QFile file(oldFileName);
// QDir::setCurrent(fi.absolutePath());
// if ( !file.rename(newFileName + ".remarkup") ) {
// QMessageBox::warning(this, tr("Warning"),
// tr("Rename failed: file '%1' already exists").arg(newFileName));
// } else {
// emit fileMoved(fi.absoluteDir().absolutePath() + QDir::separator() + newFileName + ".remarkup", oldFileName);
// }
// }
}
void TreeView::addChildTopic(){
QString newTopicName = QInputDialog::getText(this, tr("New topic"), tr("Topic name"));
if (newTopicName.isEmpty())
return;
QFile file(__model->filePath(currentIndex()) + '/' + newTopicName + ".remarkup");
file.open(QIODevice::WriteOnly);
file.close();
}
void TreeView::addChildSection(){
QString newDirName = QInputDialog::getText(this, tr("New section"), tr("Section name"));
if (newDirName.isEmpty())
return;
QDir curDir(__model->filePath(currentIndex()));
if ( !curDir.mkdir(newDirName) ) {
QMessageBox::warning(this, tr("Warning"),
tr("Couldn't create a section named '%1'").arg(newDirName));
}
}

Event Timeline