Start review of saved search/replace

This commit is contained in:
Charles Haley 2011-01-24 10:42:36 +00:00
commit 9c1d660aa8
2 changed files with 236 additions and 24 deletions

View File

@ -6,7 +6,8 @@ __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
import re, os import re, os
from PyQt4.Qt import Qt, QDialog, QGridLayout, QVBoxLayout, QFont, QLabel, \ from PyQt4.Qt import Qt, QDialog, QGridLayout, QVBoxLayout, QFont, QLabel, \
pyqtSignal, QDialogButtonBox, QDate, QLineEdit pyqtSignal, QDialogButtonBox, QInputDialog, QLineEdit, \
QMessageBox, QDate, QLineEdit
from calibre.gui2.dialogs.metadata_bulk_ui import Ui_MetadataBulkDialog from calibre.gui2.dialogs.metadata_bulk_ui import Ui_MetadataBulkDialog
from calibre.gui2.dialogs.tag_editor import TagEditor from calibre.gui2.dialogs.tag_editor import TagEditor
@ -16,7 +17,7 @@ from calibre.ebooks.metadata.meta import get_metadata
from calibre.gui2.custom_column_widgets import populate_metadata_page from calibre.gui2.custom_column_widgets import populate_metadata_page
from calibre.gui2 import error_dialog, ResizableDialog, UNDEFINED_QDATE from calibre.gui2 import error_dialog, ResizableDialog, UNDEFINED_QDATE
from calibre.gui2.progress_indicator import ProgressIndicator from calibre.gui2.progress_indicator import ProgressIndicator
from calibre.utils.config import dynamic from calibre.utils.config import dynamic, JSONConfig
from calibre.utils.titlecase import titlecase from calibre.utils.titlecase import titlecase
from calibre.utils.icu import sort_key, capitalize from calibre.utils.icu import sort_key, capitalize
from calibre.utils.config import prefs, tweaks from calibre.utils.config import prefs, tweaks
@ -451,6 +452,18 @@ class MetadataBulkDialog(ResizableDialog, Ui_MetadataBulkDialog):
self.results_count.valueChanged[int].connect(self.s_r_display_bounds_changed) self.results_count.valueChanged[int].connect(self.s_r_display_bounds_changed)
self.starting_from.valueChanged[int].connect(self.s_r_display_bounds_changed) self.starting_from.valueChanged[int].connect(self.s_r_display_bounds_changed)
self.save_button.clicked.connect(self.save_query)
self.remove_button.clicked.connect(self.remove_query)
self.query_field.currentIndexChanged[str].connect(self.query_change)
self.queries = JSONConfig("queries")
self.query_field.addItem("")
for item in self.queries:
self.query_field.addItem(item)
self.query_field.setCurrentIndex(0)
def s_r_get_field(self, mi, field): def s_r_get_field(self, mi, field):
if field: if field:
if field == '{template}': if field == '{template}':
@ -862,3 +875,105 @@ class MetadataBulkDialog(ResizableDialog, Ui_MetadataBulkDialog):
def series_changed(self, *args): def series_changed(self, *args):
self.write_series = True self.write_series = True
def remove_query(self, *args):
if not self.query_field.currentText():
return
ret = QMessageBox.question(self, _("Delete query"),
_("Selected query will be deleted. Are You sure?"),
QMessageBox.Ok, QMessageBox.Cancel)
if ret == QMessageBox.Cancel:
return
item_id = self.query_field.currentIndex()
item_name = self.query_field.currentText()
self.query_field.removeItem(item_id)
if item_name in self.queries.keys():
del(self.queries[str(item_name)])
self.queries.commit()
def save_query(self, *args):
query = {}
query['name'], ok = QInputDialog.getText(self, _('Save query'),
_('Query name:'))
if not ok:
return
query['name'] = str(query['name'])
new = True
if query['name'] in self.queries.keys():
ret = QMessageBox.question(self, _("Save query"),
_("Query already exists, old one would be overwritten." \
" Are You sure?"),
QMessageBox.Ok, QMessageBox.Cancel)
if ret == QMessageBox.Cancel:
return
new = False
query['search_field'] = str(self.search_field.currentText())
query['search_mode'] = str(self.search_mode.currentText())
query['s_r_template'] = str(self.s_r_template.text())
query['search_for'] = str(self.search_for.text())
query['case_sensitive'] = self.case_sensitive.isChecked()
query['replace_with'] = str(self.replace_with.text())
query['replace_func'] = str(self.replace_func.currentText())
query['destination_field'] = str(self.destination_field. \
currentText())
query['replace_mode'] = str(self.replace_mode.currentText())
query['comma_separated'] = self.comma_separated.isChecked()
query['results_count'] = self.results_count.value()
query['starting_from'] = self.starting_from.value()
query['multiple_separator'] = str(self.multiple_separator.text())
self.queries[query['name']] = query
self.queries.commit()
if new:
self.query_field.addItem(query['name'])
self.query_field.setCurrentIndex(self.query_field.findText(query['name']))
def query_change(self, item_name):
item = self.queries.get(str(item_name), None)
if item is None:
self.reset_query_fields()
return
self.search_field.setCurrentIndex(
self.search_field.findText(item['search_field']))
self.search_mode.setEditText(item['search_mode'])
self.search_mode.setCurrentIndex(
self.search_mode.findText(item['search_mode']))
self.s_r_template.setText(item['search_mode'])
self.search_for.setText(item['search_for'])
self.case_sensitive.setChecked(item['case_sensitive'])
self.replace_with.setText(item['replace_with'])
self.replace_func.setCurrentIndex(
self.replace_func.findText(item['replace_func']))
self.destination_field.setCurrentIndex(
self.destination_field.findText(item['destination_field']))
self.replace_mode.setCurrentIndex(
self.replace_mode.findText(item['replace_mode']))
self.comma_separated.setChecked(item['comma_separated'])
self.results_count.setValue(int(item['results_count']))
self.starting_from.setValue(int(item['starting_from']))
self.multiple_separator.setText(item['multiple_separator'])
def reset_query_fields(self):
self.search_field.setCurrentIndex(0)
self.search_mode.setEditText("")
self.search_mode.setCurrentIndex(0)
self.s_r_template.setText("")
self.search_for.setText("")
self.case_sensitive.setChecked(False)
self.replace_with.setText("")
self.replace_func.setCurrentIndex(0)
self.destination_field.setCurrentIndex(0)
self.replace_mode.setCurrentIndex(0)
self.comma_separated.setChecked(True)
self.results_count.setValue(999)
self.starting_from.setValue(1)
self.multiple_separator.setText(" ::: ")

View File

@ -44,8 +44,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>842</width> <width>832</width>
<height>589</height> <height>574</height>
</rect> </rect>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_2"> <layout class="QVBoxLayout" name="verticalLayout_2">
@ -55,7 +55,7 @@
<item> <item>
<widget class="QTabWidget" name="central_widget"> <widget class="QTabWidget" name="central_widget">
<property name="currentIndex"> <property name="currentIndex">
<number>0</number> <number>2</number>
</property> </property>
<widget class="QWidget" name="tabWidgetPage1"> <widget class="QWidget" name="tabWidgetPage1">
<attribute name="title"> <attribute name="title">
@ -574,7 +574,7 @@ Future conversion of these books will use the default settings.</string>
<property name="sizeConstraint"> <property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum> <enum>QLayout::SetMinimumSize</enum>
</property> </property>
<item row="1" column="0" colspan="3"> <item row="1" column="0" colspan="4">
<widget class="QLabel" name="s_r_heading"> <widget class="QLabel" name="s_r_heading">
<property name="wordWrap"> <property name="wordWrap">
<bool>true</bool> <bool>true</bool>
@ -591,7 +591,7 @@ Future conversion of these books will use the default settings.</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="3" column="0"> <item row="11" column="0">
<widget class="QLabel" name="xlabel_21"> <widget class="QLabel" name="xlabel_21">
<property name="text"> <property name="text">
<string>Search &amp;field:</string> <string>Search &amp;field:</string>
@ -601,14 +601,14 @@ Future conversion of these books will use the default settings.</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="3" column="1"> <item row="11" column="2">
<widget class="QComboBox" name="search_field"> <widget class="QComboBox" name="search_field">
<property name="toolTip"> <property name="toolTip">
<string>The name of the field that you want to search</string> <string>The name of the field that you want to search</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="3" column="2"> <item row="11" column="3">
<layout class="QHBoxLayout" name="HLayout_3"> <layout class="QHBoxLayout" name="HLayout_3">
<item> <item>
<widget class="QLabel" name="xlabel_24"> <widget class="QLabel" name="xlabel_24">
@ -642,7 +642,7 @@ Future conversion of these books will use the default settings.</string>
</item> </item>
</layout> </layout>
</item> </item>
<item row="4" column="0"> <item row="12" column="0">
<widget class="QLabel" name="template_label"> <widget class="QLabel" name="template_label">
<property name="text"> <property name="text">
<string>Te&amp;mplate:</string> <string>Te&amp;mplate:</string>
@ -652,7 +652,7 @@ Future conversion of these books will use the default settings.</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="4" column="1"> <item row="12" column="2">
<widget class="HistoryLineEdit" name="s_r_template"> <widget class="HistoryLineEdit" name="s_r_template">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed"> <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
@ -665,7 +665,7 @@ Future conversion of these books will use the default settings.</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="5" column="0"> <item row="13" column="0">
<widget class="QLabel" name="xlabel_2"> <widget class="QLabel" name="xlabel_2">
<property name="text"> <property name="text">
<string>&amp;Search for:</string> <string>&amp;Search for:</string>
@ -675,7 +675,7 @@ Future conversion of these books will use the default settings.</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="5" column="1"> <item row="13" column="2">
<widget class="HistoryLineEdit" name="search_for"> <widget class="HistoryLineEdit" name="search_for">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed"> <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
@ -688,7 +688,7 @@ Future conversion of these books will use the default settings.</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="5" column="2"> <item row="13" column="3">
<widget class="QCheckBox" name="case_sensitive"> <widget class="QCheckBox" name="case_sensitive">
<property name="toolTip"> <property name="toolTip">
<string>Check this box if the search string must match exactly upper and lower case. Uncheck it if case is to be ignored</string> <string>Check this box if the search string must match exactly upper and lower case. Uncheck it if case is to be ignored</string>
@ -701,7 +701,7 @@ Future conversion of these books will use the default settings.</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="6" column="0"> <item row="14" column="0">
<widget class="QLabel" name="xlabel_4"> <widget class="QLabel" name="xlabel_4">
<property name="text"> <property name="text">
<string>&amp;Replace with:</string> <string>&amp;Replace with:</string>
@ -711,14 +711,14 @@ Future conversion of these books will use the default settings.</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="6" column="1"> <item row="14" column="2">
<widget class="HistoryLineEdit" name="replace_with"> <widget class="HistoryLineEdit" name="replace_with">
<property name="toolTip"> <property name="toolTip">
<string>The replacement text. The matched search text will be replaced with this string</string> <string>The replacement text. The matched search text will be replaced with this string</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="6" column="2"> <item row="14" column="3">
<layout class="QHBoxLayout" name="verticalLayout"> <layout class="QHBoxLayout" name="verticalLayout">
<item> <item>
<widget class="QLabel" name="label_41"> <widget class="QLabel" name="label_41">
@ -753,7 +753,7 @@ field is processed. In regular expression mode, only the matched text is process
</item> </item>
</layout> </layout>
</item> </item>
<item row="7" column="0"> <item row="15" column="0">
<widget class="QLabel" name="destination_field_label"> <widget class="QLabel" name="destination_field_label">
<property name="text"> <property name="text">
<string>&amp;Destination field:</string> <string>&amp;Destination field:</string>
@ -763,7 +763,7 @@ field is processed. In regular expression mode, only the matched text is process
</property> </property>
</widget> </widget>
</item> </item>
<item row="7" column="1"> <item row="15" column="2">
<widget class="QComboBox" name="destination_field"> <widget class="QComboBox" name="destination_field">
<property name="toolTip"> <property name="toolTip">
<string>The field that the text will be put into after all replacements. <string>The field that the text will be put into after all replacements.
@ -771,7 +771,7 @@ If blank, the source field is used if the field is modifiable</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="7" column="2"> <item row="15" column="3">
<layout class="QHBoxLayout" name="verticalLayout"> <layout class="QHBoxLayout" name="verticalLayout">
<item> <item>
<widget class="QLabel" name="replace_mode_label"> <widget class="QLabel" name="replace_mode_label">
@ -820,7 +820,7 @@ not multiple and the destination field is multiple</string>
</item> </item>
</layout> </layout>
</item> </item>
<item row="8" column="1" colspan="2"> <item row="16" column="2" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout_21"> <layout class="QHBoxLayout" name="horizontalLayout_21">
<item> <item>
<spacer name="HSpacer_347"> <spacer name="HSpacer_347">
@ -906,7 +906,7 @@ not multiple and the destination field is multiple</string>
</item> </item>
</layout> </layout>
</item> </item>
<item row="9" column="0" colspan="4"> <item row="17" column="0" colspan="5">
<widget class="QScrollArea" name="scrollArea11"> <widget class="QScrollArea" name="scrollArea11">
<property name="frameShape"> <property name="frameShape">
<enum>QFrame::NoFrame</enum> <enum>QFrame::NoFrame</enum>
@ -919,8 +919,8 @@ not multiple and the destination field is multiple</string>
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>197</width> <width>810</width>
<height>60</height> <height>264</height>
</rect> </rect>
</property> </property>
<layout class="QGridLayout" name="testgrid"> <layout class="QGridLayout" name="testgrid">
@ -968,6 +968,83 @@ not multiple and the destination field is multiple</string>
</widget> </widget>
</widget> </widget>
</item> </item>
<item row="3" column="0">
<widget class="QLabel" name="xlabel_22">
<property name="text">
<string>Load query:</string>
</property>
<property name="buddy">
<cstring>search_field</cstring>
</property>
</widget>
</item>
<item row="3" column="2">
<widget class="QComboBox" name="query_field">
<property name="toolTip">
<string>Select query to load.</string>
</property>
</widget>
</item>
<item row="4" column="0" colspan="4">
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="3" column="3">
<layout class="QHBoxLayout" name="horizontalLayout_6">
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="save_button">
<property name="toolTip">
<string>Save current query.</string>
</property>
<property name="text">
<string>Save</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="remove_button">
<property name="toolTip">
<string>Remove loadded query.</string>
</property>
<property name="text">
<string>Remove</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout> </layout>
</widget> </widget>
</widget> </widget>
@ -1030,6 +1107,9 @@ not multiple and the destination field is multiple</string>
<tabstop>series_numbering_restarts</tabstop> <tabstop>series_numbering_restarts</tabstop>
<tabstop>series_start_number</tabstop> <tabstop>series_start_number</tabstop>
<tabstop>button_box</tabstop> <tabstop>button_box</tabstop>
<tabstop>query_field</tabstop>
<tabstop>save_button</tabstop>
<tabstop>remove_button</tabstop>
<tabstop>search_field</tabstop> <tabstop>search_field</tabstop>
<tabstop>search_mode</tabstop> <tabstop>search_mode</tabstop>
<tabstop>s_r_template</tabstop> <tabstop>s_r_template</tabstop>
@ -1045,6 +1125,23 @@ not multiple and the destination field is multiple</string>
<tabstop>multiple_separator</tabstop> <tabstop>multiple_separator</tabstop>
<tabstop>test_text</tabstop> <tabstop>test_text</tabstop>
<tabstop>test_result</tabstop> <tabstop>test_result</tabstop>
<tabstop>scrollArea</tabstop>
<tabstop>central_widget</tabstop>
<tabstop>swap_title_and_author</tabstop>
<tabstop>clear_series</tabstop>
<tabstop>adddate</tabstop>
<tabstop>clear_adddate_button</tabstop>
<tabstop>apply_adddate</tabstop>
<tabstop>pubdate</tabstop>
<tabstop>clear_pubdate_button</tabstop>
<tabstop>apply_pubdate</tabstop>
<tabstop>remove_format</tabstop>
<tabstop>change_title_to_title_case</tabstop>
<tabstop>remove_conversion_settings</tabstop>
<tabstop>cover_generate</tabstop>
<tabstop>cover_remove</tabstop>
<tabstop>cover_from_fmt</tabstop>
<tabstop>scrollArea11</tabstop>
</tabstops> </tabstops>
<resources> <resources>
<include location="../../../../resources/images.qrc"/> <include location="../../../../resources/images.qrc"/>