-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
561 lines (447 loc) · 18.6 KB
/
main.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
""" An application to rename files. """
# Python imports
import os # Miscellaneous operating system interfaces
import sys # System-specific parameters and functions
# PyQt imports
from PyQt4 import QtCore, QtGui
# Application classes
from filelisthandler import FileListHandler # A class to handle file lists
# Import mainwindow
from mainwindow import *
# Create a class for our mainwindow
class Main(QtGui.QMainWindow):
# Initialize mainwindow
def __init__(self):
# Declare class variables
self.path = "" # String for path
# Create class instances
self.filelist = FileListHandler()
# Initialize top level window widget
QtGui.QMainWindow.__init__(self)
# This is always the same
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# Connect signals, menu and toolbar
self.ui.actionAddFiles.triggered.connect(self.addFiles)
self.ui.actionAddFolder.triggered.connect(self.addFolder)
self.ui.actionRemoveFiles.triggered.connect(self.removeFiles)
self.ui.actionClearList.triggered.connect(self.clearList)
self.ui.actionRename.triggered.connect(self.renameFiles)
self.ui.actionQuit.triggered.connect(self.quitApplication)
self.ui.actionAbout.triggered.connect(self.aboutMessage)
# Connect signals, tab widget
self.ui.tabMain.currentChanged.connect(self.changeTab)
# Connect signals, counter
self.ui.sboCounterPos.valueChanged.connect(self.addCounter)
self.ui.cboCounterSide.activated.connect(self.addCounter)
self.ui.sboCounterZeros.valueChanged.connect(self.addCounter)
self.ui.chkCounterReplace.stateChanged.connect(self.addCounter)
self.ui.btnCounter.clicked.connect(self.addCounter)
# Connect signals, crop
self.ui.sboCropStart.valueChanged.connect(self.addCrop)
self.ui.sboCropEnd.valueChanged.connect(self.addCrop)
self.ui.chkCropInvert.stateChanged.connect(self.addCrop)
self.ui.btnCrop.clicked.connect(self.addCrop)
# Connect signals, extension
self.ui.txtExt.textEdited.connect(self.addExt)
self.ui.btnExt.clicked.connect(self.addExt)
# Connect signals, insert
self.ui.txtInsertText.textEdited.connect(self.addInsert)
self.ui.sboInsertPos.valueChanged.connect(self.addInsert)
self.ui.cboInsertSide.activated.connect(self.addInsert)
self.ui.btnInsert.clicked.connect(self.addInsert)
# Connect signals, regex
self.ui.txtRegExSearch.textEdited.connect(self.addRegEx)
self.ui.txtRegExReplace.textEdited.connect(self.addRegEx)
self.ui.btnRegEx.clicked.connect(self.addRegEx)
# Connect signals, replace
self.ui.txtReplaceSearch.textEdited.connect(self.addReplace)
self.ui.txtReplaceWith.textEdited.connect(self.addReplace)
self.ui.btnReplace.clicked.connect(self.addReplace)
# Connect signals, text case
self.ui.optTextCase1.clicked.connect(self.addTextCase)
self.ui.optTextCase2.clicked.connect(self.addTextCase)
self.ui.optTextCase3.clicked.connect(self.addTextCase)
self.ui.optTextCase4.clicked.connect(self.addTextCase)
self.ui.btnTextCase.clicked.connect(self.addTextCase)
# Connect signals, trim
self.ui.sboTrimLength.valueChanged.connect(self.addTrim)
self.ui.cboTrimSide.activated.connect(self.addTrim)
self.ui.btnTrim.clicked.connect(self.addTrim)
# Icons
self.setWindowIcon(QtGui.QIcon("icon.png"))
self.ui.actionAddFiles.setIcon(QtGui.QIcon("add_files.png"))
self.ui.actionAddFolder.setIcon(QtGui.QIcon("add_folder.png"))
self.ui.actionRemoveFiles.setIcon(QtGui.QIcon("remove.png"))
self.ui.actionClearList.setIcon(QtGui.QIcon("clear.png"))
self.ui.actionRename.setIcon(QtGui.QIcon("rename.png"))
self.ui.actionQuit.setIcon(QtGui.QIcon("quit.png"))
self.ui.actionAbout.setIcon(QtGui.QIcon("about.png"))
self.ui.tabMain.setTabIcon(0, QtGui.QIcon("counter.png"))
self.ui.tabMain.setTabIcon(1, QtGui.QIcon("crop.png"))
self.ui.tabMain.setTabIcon(2, QtGui.QIcon("extension.png"))
self.ui.tabMain.setTabIcon(3, QtGui.QIcon("insert.png"))
self.ui.tabMain.setTabIcon(4, QtGui.QIcon("regex.png"))
self.ui.tabMain.setTabIcon(5, QtGui.QIcon("replace.png"))
self.ui.tabMain.setTabIcon(6, QtGui.QIcon("text_case.png"))
self.ui.tabMain.setTabIcon(7, QtGui.QIcon("trim.png"))
# Drag-and-drop events for file list
self.ui.tblFileList.dragEnterEvent = self.dragEnterEvent
self.ui.tblFileList.dragMoveEvent = self.dragEnterEvent
self.ui.tblFileList.dropEvent = self.dropEvent
# Select the first tab from the tab widget
self.ui.tabMain.setCurrentIndex(0)
# Load settings
self.loadSettings()
# Check if command line arguments has files in it
if sys.argv[1:]:
self.loopItems(sys.argv[1:])
# Refresh table
self.refreshTable()
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#+ Actions
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# Add files
def addFiles(self):
# Get file list using a dialog
items = QtGui.QFileDialog.getOpenFileNames(self, "Add files",
self.path)
# Check list for items
if len(items) < 1:
return
# Loop items to main file list
self.loopItems(items)
# Add folder
def addFolder(self):
# Get folder using a dialog
path = QtGui.QFileDialog.getExistingDirectory(self, "Add folder",
self.path)
# Check path
if path == "":
return
# Check dir
if not os.path.isdir(path):
return
# Create a list with full paths
items = []
for item in os.listdir(path):
items.append(os.path.join(path, item))
# Loop items to main file list
self.loopItems(items)
# Loop items to main file list
def loopItems(self, items):
nonexist = 0
duplicates = 0
folders = 0
for item in items:
# Check if item does not exist
if not os.path.exists(item):
nonexist += 1
continue
# Check for duplicates
if self.filelist.exists(item):
duplicates += 1
continue
# Check for folders
if os.path.isdir(item):
folders += 1
continue
# Check if file path is an existing regular file
if not os.path.isfile(item):
continue
# Set the path from first file
if item == items[0]:
self.path = os.path.split(item)[0]
# Add file
self.filelist.addfile(item)
# Check for files, sort them, enable widgets and refresh table
if self.filelist.count > 0:
self.filelist.sort()
self.enableWidgets()
self.refreshTable()
# Count total adds and message user, if necessary
total = nonexist
total += duplicates
total += folders
if total > 0:
msg = "%s items were skipped:\n" % (total)
msg += "\n"
msg += "%s doesn't exist\n" % (nonexist)
msg += "%s duplicates\n" % (duplicates)
msg += "%s folders\n" % (folders)
QtGui.QMessageBox.information(self, "Info", msg)
# Clear list
def clearList(self):
self.filelist.clear()
self.disableWidgets()
self.refreshTable()
# Remove files
def removeFiles(self):
# Check if file list is empty
if self.filelist.count < 1:
msg = "No files in list."
QtGui.QMessageBox.critical(self, "Error", msg)
return
# Check table's selected items
if not self.ui.tblFileList.selectedIndexes():
msg = "Please select file."
QtGui.QMessageBox.critical(self, "Error", msg)
return
# Get indexes from table
rows = []
for item in self.ui.tblFileList.selectedIndexes():
index = int(item.row())
if index not in rows:
rows.append(index)
# Remove items in reverse order (so the indexes won't change)
for index in sorted(rows, reverse=True):
self.filelist.remove(index)
# Check file list for files
if self.filelist.count < 1:
self.disableWidgets()
# Refresh table
self.refreshTable()
# Rename files
def renameFiles(self):
ok = self.filelist.rename()
self.filelist.reset()
self.refreshTable()
if ok:
message = "Files renamed successfully."
QtGui.QMessageBox.information(self, "Info", message)
else:
QtGui.QMessageBox.critical(self, "Error", self.filelist.error)
# Quit
def quitApplication(self):
QtGui.QApplication.quit()
# Help > about...
def aboutMessage(self):
msg = """<strong>bwRenamer</strong><br />
Version 1.1.0<br />
<br />
This is free software.<br />
Released under the General Public License.<br />
<br />
<a href="https://github.com/bulkware/bwrenamer">GitHub</a>"""
QtGui.QMessageBox.about(self, "About", msg)
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#+ Events
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# Drag
def dragEnterEvent(self, event):
if (event.type() == QtCore.QEvent.DragEnter):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
# Drop
def dropEvent(self, event):
if (event.type() == QtCore.QEvent.Drop):
if event.mimeData().hasUrls():
# Make a list of items from drag-and-drop
items = []
for i in event.mimeData().urls():
items.append(i.toLocalFile())
# Loop items to main file list
self.loopItems(items)
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#+ Settings
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
def loadSettings(self):
try:
settings = QtCore.QSettings("bulkware", "bwRenamer")
if settings.contains("geometry"): # Window geometry
self.restoreGeometry(settings.value("geometry"))
if settings.contains("state"): # Window state
self.restoreState(settings.value("state"))
if settings.contains("path"):
self.path = settings.value("path", type=str)
if settings.contains("tabindex"):
tabindex = settings.value("tabindex", type=int)
self.ui.tabMain.setCurrentIndex(tabindex)
except:
self.path = ""
self.ui.tabMain.setCurrentIndex(0)
return False
else:
return True
# Save settings when closing the application
def closeEvent(self, event):
settings = QtCore.QSettings("bulkware", "bwRenamer")
settings.setValue("geometry", self.saveGeometry())
settings.setValue("state", self.saveState())
settings.setValue("path", self.path)
settings.setValue("tabindex", self.ui.tabMain.currentIndex())
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#+ Widgets
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# Tab widget
def changeTab(self, index):
if self.filelist.count < 1: return
tabname = str(self.ui.tabMain.tabText(index))
if tabname == "Counter": self.addCounter()
elif tabname == "Crop": self.addCrop()
elif tabname == "Extension": self.addExt()
elif tabname == "Insert": self.addInsert()
elif tabname == "RegEx": self.addRegEx()
elif tabname == "Replace": self.addReplace()
elif tabname == "Text case": self.addTextCase()
elif tabname == "Trim": self.addTrim()
# Counter
def addCounter(self):
preview = True
if self.sender() and str(self.sender().objectName()) == "btnCounter":
preview = False
pos = int(self.ui.sboCounterPos.text())
side = int(self.ui.cboCounterSide.currentIndex())
zeros = int(self.ui.sboCounterZeros.text()) + 1
if self.ui.chkCounterReplace.isChecked():
replace = True
else:
replace = False
ok = self.filelist.counter(pos, side, zeros, replace, preview)
if not ok and not preview:
QtGui.QMessageBox.critical(self, "Error", self.filelist.error)
self.refreshTable()
# Crop
def addCrop(self):
preview = True
if self.sender() and str(self.sender().objectName()) == "btnCrop":
preview = False
start = int(self.ui.sboCropStart.text())
end = int(self.ui.sboCropEnd.text())
if self.ui.chkCropInvert.isChecked():
invert = True
else:
invert = False
ok = self.filelist.crop(start, end, invert, preview)
if not ok and not preview:
QtGui.QMessageBox.critical(self, "Error", self.filelist.error)
self.refreshTable()
# Extension
def addExt(self):
preview = True
if self.sender() and str(self.sender().objectName()) == "btnExt":
preview = False
ext = self.ui.txtExt.text()
ok = self.filelist.extension(ext, preview)
if not ok and not preview:
QtGui.QMessageBox.critical(self, "Error", self.filelist.error)
self.refreshTable()
# Insert
def addInsert(self):
preview = True
if self.sender() and str(self.sender().objectName()) == "btnInsert":
preview = False
text = self.ui.txtInsertText.text()
pos = int(self.ui.sboInsertPos.text())
side = int(self.ui.cboInsertSide.currentIndex())
ok = self.filelist.insert(text, pos, side, preview)
if not ok and not preview:
QtGui.QMessageBox.critical(self, "Error", self.filelist.error)
self.refreshTable()
# RegEx
def addRegEx(self):
preview = True
if self.sender() and str(self.sender().objectName()) == "btnRegEx":
preview = False
old = self.ui.txtRegExSearch.text()
new = self.ui.txtRegExReplace.text()
ok = self.filelist.regex(old, new, preview)
if not ok and not preview:
QtGui.QMessageBox.critical(self, "Error", self.filelist.error)
self.refreshTable()
# Replace
def addReplace(self):
preview = True
if self.sender() and str(self.sender().objectName()) == "btnReplace":
preview = False
old = self.ui.txtReplaceSearch.text()
new = self.ui.txtReplaceWith.text()
ok = self.filelist.replace(old, new, preview)
if not ok and not preview:
QtGui.QMessageBox.critical(self, "Error", self.filelist.error)
self.refreshTable()
# Text case
def addTextCase(self):
preview = True
if self.sender() and str(self.sender().objectName()) == "btnTextCase":
preview = False
if self.ui.optTextCase1.isChecked():
mode = 0
elif self.ui.optTextCase2.isChecked():
mode = 1
elif self.ui.optTextCase3.isChecked():
mode = 2
elif self.ui.optTextCase4.isChecked():
mode = 3
ok = self.filelist.textcase(mode, preview)
if not ok and not preview:
QtGui.QMessageBox.critical(self, "Error", self.filelist.error)
self.refreshTable()
# Trim
def addTrim(self):
preview = True
if self.sender() and str(self.sender().objectName()) == "btnTrim":
preview = False
length = int(self.ui.sboTrimLength.text())
side = int(self.ui.cboTrimSide.currentIndex())
ok = self.filelist.trim(length, side, preview)
if not ok and not preview:
QtGui.QMessageBox.critical(self, "Error", self.filelist.error)
self.refreshTable()
# Refresh table
def refreshTable(self):
# Clear widgets
self.ui.tblFileList.setColumnCount(0)
self.ui.tblFileList.setRowCount(0)
self.ui.tblFileList.clear()
# Check if file list is empty
if self.filelist.count < 1:
return
# Set columns and rows
self.ui.tblFileList.setColumnCount(4)
self.ui.tblFileList.setRowCount(self.filelist.count)
# Set header labels
self.ui.tblFileList.setHorizontalHeaderLabels(["Original", "Modified", \
"Preview", ""])
# Populate table
for i, file in enumerate(self.filelist.filelist):
item = QtGui.QTableWidgetItem(file[2])
item.setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignLeft)
self.ui.tblFileList.setItem(i, 0, item)
item = QtGui.QTableWidgetItem(file[3])
item.setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignLeft)
self.ui.tblFileList.setItem(i, 1, item)
item = QtGui.QTableWidgetItem(file[4])
item.setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignLeft)
self.ui.tblFileList.setItem(i, 2, item)
item = QtGui.QTableWidgetItem()
item.setFlags(QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable)
self.ui.tblFileList.setItem(i, 3, item)
# Resize columns to contents
# setVisible lines are because of QTBUG-9352!
self.ui.tblFileList.setVisible(False)
self.ui.tblFileList.resizeColumnsToContents()
self.ui.tblFileList.setVisible(True)
# Disable widgets
def disableWidgets(self):
self.ui.actionRemoveFiles.setEnabled(False)
self.ui.actionClearList.setEnabled(False)
self.ui.actionRename.setEnabled(False)
self.ui.tabMain.setEnabled(False)
# Enable widgets
def enableWidgets(self):
self.ui.actionRemoveFiles.setEnabled(True)
self.ui.actionClearList.setEnabled(True)
self.ui.actionRename.setEnabled(True)
self.ui.tabMain.setEnabled(True)
# Creates an application object and begins the event handling loop
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = Main()
window.show()
ret = app.exec_()
sys.exit(ret)