Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement open recent file menu #1834

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions resources/ui/carla_host.ui
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,16 @@
<property name="spacing">
<number>0</number>
</property>
<property name="margin">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
Expand Down Expand Up @@ -463,17 +472,23 @@
<x>0</x>
<y>0</y>
<width>1045</width>
<height>25</height>
<height>30</height>
</rect>
</property>
<widget class="QMenu" name="menu_File">
<property name="title">
<string>&amp;File</string>
</property>
<widget class="QMenu" name="menu_Open_Recent">
<property name="title">
<string>Open Recent...</string>
</property>
</widget>
<addaction name="act_file_connect"/>
<addaction name="act_file_refresh"/>
<addaction name="act_file_new"/>
<addaction name="act_file_open"/>
<addaction name="menu_Open_Recent"/>
<addaction name="act_file_save"/>
<addaction name="act_file_save_as"/>
<addaction name="separator"/>
Expand Down Expand Up @@ -1008,7 +1023,7 @@
<property name="currentIndex">
<number>0</number>
</property>
<property name="tabBarAutoHide" stdset="0">
<property name="tabBarAutoHide">
<bool>false</bool>
</property>
<widget class="QWidget" name="tab_3">
Expand Down
73 changes: 73 additions & 0 deletions source/frontend/carla_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ def __init__(self, host, withCanvas, parent=None):
self.fCurrentlyRemovingAllPlugins = False
self.fHasLoadedLv2Plugins = False

self.fRecentFileList = []

self.fLastTransportBPM = 0.0
self.fLastTransportFrame = 0
self.fLastTransportState = False
Expand Down Expand Up @@ -249,6 +251,7 @@ def __init__(self, host, withCanvas, parent=None):
if self.host.isControl:
self.ui.act_file_new.setVisible(False)
self.ui.act_file_open.setVisible(False)
self.ui.menu_Open_Recent.setVisible(False)
self.ui.act_file_save_as.setVisible(False)
self.ui.tabUtils.removeTab(0)
else:
Expand All @@ -275,6 +278,7 @@ def __init__(self, host, withCanvas, parent=None):
self.ui.act_file_new.setEnabled(False)

self.ui.act_file_open.setEnabled(False)
self.ui.menu_Open_Recent.setEnabled(False)
self.ui.act_file_save.setEnabled(False)
self.ui.act_file_save_as.setEnabled(False)
self.ui.act_engine_stop.setEnabled(False)
Expand Down Expand Up @@ -438,6 +442,7 @@ def __init__(self, host, withCanvas, parent=None):
# Load Settings

self.loadSettings(True)
self.updateRecentFiles()

# ----------------------------------------------------------------------------------------------------
# Set-up Canvas
Expand All @@ -460,6 +465,7 @@ def __init__(self, host, withCanvas, parent=None):
self.ui.act_file_refresh.setIcon(getIcon('view-refresh', 16, 'svgz'))
self.ui.act_file_new.setIcon(getIcon('document-new', 16, 'svgz'))
self.ui.act_file_open.setIcon(getIcon('document-open', 16, 'svgz'))
self.ui.menu_Open_Recent.setIcon(getIcon('document-open', 16, 'svgz'))
self.ui.act_file_save.setIcon(getIcon('document-save', 16, 'svgz'))
self.ui.act_file_save_as.setIcon(getIcon('document-save-as', 16, 'svgz'))
self.ui.act_file_quit.setIcon(getIcon('application-exit', 16, 'svgz'))
Expand Down Expand Up @@ -897,6 +903,70 @@ def slot_fileOpen(self):
self.loadProjectNow()
self.fProjectFilename = filenameOld

self.addToRecentFilesList(self.fProjectFilename)

def addToRecentFilesList(self, filePath):
# FIXME: move this constant elsewhere? or add it to global settings?
MAX_RECENT_FILES = 4
settings = QSafeSettings()
recentFileList = settings.value("RecentFileList", [], list)

# TODO: reorder the list so last used are listed first?
if filePath not in recentFileList:
recentFileList.insert(0, filePath)
if len(recentFileList) > MAX_RECENT_FILES:
recentFileList.pop()

settings.setValue("RecentFileList", recentFileList)
del settings
self.updateRecentFiles()

def updateRecentFiles(self):
settings = QSafeSettings()
recentFileList = settings.value("RecentFileList", [], list)
del settings

menu = self.ui.menu_Open_Recent
menu.clear()

for f in recentFileList:
act = menu.addAction(f)
act.triggered.connect(self.slot_fileMenuOpenRecent)
act = menu.addSeparator()
act = menu.addAction("Clear")
act.triggered.connect(self.slot_clearRecentFiles)

@pyqtSlot()
def slot_clearRecentFiles(self):
menu = self.ui.menu_Open_Recent
menu.clear()
settings = QSafeSettings()
settings.setValue("RecentFileList", [])
del settings

@pyqtSlot()
def slot_fileMenuOpenRecent(self):
sender = self.sender()
filename = sender.text()

newFile = True

if self.fPluginCount > 0:
ask = QMessageBox.question(self, self.tr("Question"), self.tr("There are some plugins loaded, do you want to remove them now?"),
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
newFile = (ask == QMessageBox.Yes)

if newFile:
self.pluginRemoveAll()
self.fProjectFilename = filename
self.setProperWindowTitle()
self.loadProjectNow()
else:
filenameOld = self.fProjectFilename
self.fProjectFilename = filename
self.loadProjectNow()
self.fProjectFilename = filenameOld

@pyqtSlot()
def slot_fileSave(self, saveAs=False):
if self.fProjectFilename and not saveAs:
Expand All @@ -916,6 +986,7 @@ def slot_fileSave(self, saveAs=False):
self.fProjectFilename = filename
self.setProperWindowTitle()

self.addToRecentFilesList(filename)
self.saveProjectNow()

@pyqtSlot()
Expand Down Expand Up @@ -1096,6 +1167,7 @@ def slot_handleEngineStartedCallback(self, pluginCount, processMode, transportMo

if self.host.isPlugin or not self.fSessionManagerName:
self.ui.act_file_open.setEnabled(True)
self.ui.menu_Open_Recent.setEnabled(True)
self.ui.act_file_save_as.setEnabled(True)

self.ui.cb_transport_jack.setChecked(transportMode == ENGINE_TRANSPORT_MODE_JACK)
Expand Down Expand Up @@ -1139,6 +1211,7 @@ def slot_handleEngineStoppedCallback(self):

if self.host.isPlugin or not self.fSessionManagerName:
self.ui.act_file_open.setEnabled(False)
self.ui.act_file_open_recent.setEnabled(False)
self.ui.act_file_save_as.setEnabled(False)

@pyqtSlot(int, str)
Expand Down