Skip to content

Commit

Permalink
Github Issue #12: Better focus on the RecentFilesDialog, adds the rec…
Browse files Browse the repository at this point in the history
…ently opened file on top of the order even if its opened by ctrl+tab, removed unnecessary keyReleaseEvent from RecentFilesDialog. Removed other signal and slots to open the recent files. Using a public method to obtain the path directly to open those files
  • Loading branch information
AngryFender committed Aug 21, 2023
1 parent 6297991 commit 4eaab06
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 56 deletions.
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int main(int argc, char *argv[])
&w,&MainWindow::viewChosenHandler);

QObject::connect(&filter,&AppEventFilter::openRecentFiles,
&w,&MainWindow::openRecentFilesDialog);
&w,&MainWindow::recentFilesHandler);

QObject::connect(&a, &QApplication::focusChanged, [&](QWidget *old, QWidget *now) {
QKeyEvent *event = new QKeyEvent(QKeyEvent::KeyRelease, Qt::Key_Alt, Qt::AltModifier);
Expand Down
11 changes: 5 additions & 6 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ void MainWindow::keyPressEvent(QKeyEvent *event)
}
shiftTimer->start(200);
}break;

case Qt::Key_Tab:
if( event->modifiers() == Qt::CTRL){
emit openRecentFilesDialog(true);
}break;
default:;
}
QMainWindow::keyPressEvent(event);
Expand All @@ -78,7 +73,6 @@ void MainWindow::keyReleaseEvent(QKeyEvent *event)
folderTreeBox->hide();
mkEditorBox->hide();
}break;
case Qt::Key_Control: {emit openRecentFilesDialog(false); qDebug()<<"keyrelease";break;}
}
QMainWindow::keyReleaseEvent(event);
}
Expand Down Expand Up @@ -122,6 +116,11 @@ void MainWindow::viewChosenHandler(Qt::Key key)
}
}

void MainWindow::recentFilesHandler(bool show)
{
emit openRecentFilesDialog(show);
}

void MainWindow::shiftTimerHandle()
{
shiftTimer->stop();
Expand Down
1 change: 1 addition & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class MainWindow : public QMainWindow
public slots:
void KeyPressAltHandler(bool press); //true for press, false for release
void viewChosenHandler(Qt::Key key);
void recentFilesHandler(bool show);
private slots:
void shiftTimerHandle();

Expand Down
29 changes: 16 additions & 13 deletions recentfilesdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,31 @@ void RecentFilesDialog::keyPressEvent(QKeyEvent *event)
}

listWidget->setCurrentItem(item);
currentPath = item->text();
}
QDialog::keyPressEvent(event);
}

void RecentFilesDialog::keyReleaseEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Tab){
if(listWidget->count() >0){
QListWidgetItem *item = listWidget->currentItem();
emit openFile(item->text());
}
}
QDialog::keyReleaseEvent(event);
}

void RecentFilesDialog::show()
{
QDialog::show();
listWidget->clearSelection();
listWidget->setFocusPolicy(Qt::StrongFocus);
listWidget->setFocus(Qt::MouseFocusReason);
listWidget->setCurrentRow(0,QItemSelectionModel::Select);
listWidget->activateWindow();
listWidget->setFocus();

}

const QString &RecentFilesDialog::getCurrentRelativeFile() const
{
return currentPath;
}

void RecentFilesDialog::updateRecentFileHandle(const QString &relativePath)
{
QListWidgetItem *newItem = new QListWidgetItem;
// QListWidgetItem *nextItem;
QFileIconProvider iconProvider;
newItem->setIcon(iconProvider.icon(QFileIconProvider::File));
newItem->setText(relativePath);
Expand All @@ -63,5 +61,10 @@ void RecentFilesDialog::updateRecentFileHandle(const QString &relativePath)
}
listWidget->clearSelection();
listWidget->insertItem(0,newItem);
listWidget->setCurrentItem(newItem);
// if(listWidget->count() <= 1){
// listWidget->setCurrentItem(newItem);
// listWidget->setCurrentRow(1);
// }else{
// listWidget->setCurrentRow(1);
// }
}
6 changes: 2 additions & 4 deletions recentfilesdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@ class RecentFilesDialog : public QDialog

QVBoxLayout * layout;
QListWidget *listWidget;
QString currentPath;

protected:
void keyPressEvent(QKeyEvent *event) override;
void keyReleaseEvent(QKeyEvent *event) override;

public:
RecentFilesDialog(QWidget*parent = nullptr);
void show();
const QString& getCurrentRelativeFile()const;
public slots:
void updateRecentFileHandle(const QString &relativePath);

signals:
void openFile(const QString &relativePath);
};

#endif // RECENTFILESDIALOG_H
55 changes: 24 additions & 31 deletions views_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,6 @@ void ViewsHandler::initConnection()
QObject::connect(this,&ViewsHandler::updateRecentFile,
recentFilesView,&RecentFilesDialog::updateRecentFileHandle);

QObject::connect(recentFilesView,&RecentFilesDialog::openFile,
this,&ViewsHandler::openRecentFileHandle);

}

QString ViewsHandler::getFileContent(QFile& file)
Expand Down Expand Up @@ -342,12 +339,33 @@ void ViewsHandler::openRecentFilesDialogHandle(bool show)
recentFilesView->setGeometry(viewRightFrame->geometry());
recentFilesView->move(pos);
recentFilesView->show();
recentFilesView->activateWindow();
recentFilesView->setFocus(Qt::ActiveWindowFocusReason);
}
}
else
else{
if(!recentFilesView->isHidden()){
const QString& relativePath = recentFilesView->getCurrentRelativeFile();

QString fullFilePath = vaultPath + relativePath;
fileInfo = QFileInfo(fullFilePath);
if (!fileInfo.isFile()|| !fileInfo.exists())
return;

QSharedPointer<QFile> file = QSharedPointer<QFile>(new QFile(fileInfo.absoluteFilePath()));
QString fullContent = getFileContent(*file.get());

mkGuiDocument.clear();
mkGuiDocument.setPlainText(fullContent);

viewTitle->setText(fileInfo.baseName());
viewText->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
viewText->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
viewText->update();

emit updateRecentFile(relativePath);
}
recentFilesView->hide();
}

}

void ViewsHandler::startTextSearchInAllFilesHandle()
Expand All @@ -365,28 +383,3 @@ void ViewsHandler::startTextSearchInAllFilesHandle()
textSearchAllView->setFocusAtSearch();
}
}

void ViewsHandler::openRecentFileHandle(const QString &relativePath)
{
QString fullFilePath = vaultPath + relativePath;
fileInfo = QFileInfo(fullFilePath);
if (!fileInfo.isFile()|| !fileInfo.exists())
return;

QSharedPointer<QFile> file = QSharedPointer<QFile>(new QFile(fileInfo.absoluteFilePath()));
QString fullContent = getFileContent(*file.get());

mkGuiDocument.clear();
mkGuiDocument.setPlainText(fullContent);

viewTitle->setText(fileInfo.baseName());
viewText->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
viewText->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
viewText->update();

QPair<int,int> positions = recentFileCursorMap.value(relativePath);

// QTextCursor cursor = viewText->textCursor();
// cursor.setPosition(positions.first + positions.second);
// viewText->setTextCursor(cursor);
}
1 change: 0 additions & 1 deletion views_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class ViewsHandler: public QObject
public slots:
void openRecentFilesDialogHandle(bool show);
void startTextSearchInAllFilesHandle();
void openRecentFileHandle(const QString &relativePath);

private:
ViewsHandler(QWidget*parent,Ui::MainWindow &ui){
Expand Down

0 comments on commit 4eaab06

Please sign in to comment.