Skip to content

Commit

Permalink
Added listener to open new tab (#476).
Browse files Browse the repository at this point in the history
Fixes #148.
  • Loading branch information
Mte90 authored and mitya57 committed Oct 14, 2019
1 parent b3f7936 commit ec3e50f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
18 changes: 18 additions & 0 deletions ReText/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
QFileInfo, QIODevice, QLibraryInfo, QTextStream, QTranslator, Qt
from PyQt5.QtWidgets import QApplication
from PyQt5.QtNetwork import QNetworkProxyFactory
from PyQt5.QtDBus import QDBusConnection, QDBusInterface

def canonicalize(option):
if option in ('--preview', '-'):
Expand Down Expand Up @@ -74,7 +75,10 @@ def main():
parser.addVersionOption()
previewOption = QCommandLineOption('preview',
QApplication.translate('main', 'Open the files in preview mode'))
newWindowOption = QCommandLineOption('new-window',
QApplication.translate('main', 'Create a new window even if there is an existing one'))
parser.addOption(previewOption)
parser.addOption(newWindowOption)
parser.addPositionalArgument('files',
QApplication.translate('main', 'List of files to open'),
'[files...]')
Expand All @@ -100,6 +104,20 @@ def main():
app.setStyleSheet(QTextStream(sheetfile).readAll())
sheetfile.close()
window = ReTextWindow()

connection = QDBusConnection.sessionBus()
if connection.isConnected() and not parser.isSet(newWindowOption):
connection.registerObject('/', window, QDBusConnection.ExportAllSlots)
serviceName = 'me.mitya57.ReText'
if not connection.registerService(serviceName) and filesToOpen:
print('Opening the file(s) in the existing window of ReText.')
iface = QDBusInterface(serviceName, '/', '', connection)
for fileName in filesToOpen:
iface.call('openFileWrapper', fileName)
qWidgetIface = QDBusInterface(serviceName, '/', 'org.qtproject.Qt.QWidget', connection)
qWidgetIface.call('raise')
sys.exit(0)

window.show()
# ReText can change directory when loading files, so we
# need to have a list of canonical names before loading
Expand Down
3 changes: 2 additions & 1 deletion ReText/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
enchant = None

from PyQt5.QtCore import QDir, QFile, QFileInfo, QFileSystemWatcher, \
QIODevice, QLocale, QMarginsF, QTextCodec, QTextStream, QTimer, QUrl, Qt
QIODevice, QLocale, QMarginsF, QTextCodec, QTextStream, QTimer, QUrl, Qt, pyqtSlot
from PyQt5.QtGui import QColor, QDesktopServices, QIcon, \
QKeySequence, QPageLayout, QPageSize, QPagedPaintDevice, QPalette, \
QTextDocument, QTextDocumentWriter
Expand Down Expand Up @@ -826,6 +826,7 @@ def openFile(self):
for fileName in fileNames[0]:
self.openFileWrapper(fileName)

@pyqtSlot(str)
def openFileWrapper(self, fileName):
if not fileName:
return
Expand Down

0 comments on commit ec3e50f

Please sign in to comment.