Posts Tagged ‘qt’
* History Drop Down With Model
Posted on July 23rd, 2009 by John. Filed under programming.
Following is a bit of python code that illustrates how to create a QComboBox that attaches to a model for listing history items. The main features of this code are items entered in the text area of the combo are added to the history. Selected items and items entered that already appear in the combo are moved to the top. When MAX_ITEMS is exceeded older items (items at the bottom of the drop down) are removed.
#!/usr/bin/env python from PyQt4.Qt import * from PyQt4.QtGui import * class ComboModel(QAbstractListModel): MAX_ITEMS = 5 items = [u'123', u'456', u'789'] # Required to get a working model. def rowCount(self, parent=QModelIndex()): # This is a List model meaning all elements are root elements. if parent and parent.isValid(): return 0 return len(self.items) # Required to get a working model. def insertRows(self, row, count, parent=QModelIndex()): self.beginInsertRows(parent, 0, 1) self.endInsertRows() return True def data(self, index, role): if role in (Qt.DisplayRole, Qt.EditRole): return QVariant(self.items[index.row()]) return QVariant() def setData(self, index, value, role): value = unicode(value.toString()) if value in self.items: # Move the item to the top of the list. del self.items[self.items.index(value)] self.items.insert(0, value) else: # Add the new item to the top of the list. self.items.insert(0, value) self.remove_items() self.emit(SIGNAL('dataChanged(QModelIndex, QModelIndex)'), self.createIndex(0, 0), self.createIndex(len(self.items) - 1, 0)) return True def remove_items(self): ''' Checks the number of items in the list and if it has been exceeded removes extra items. ''' if len(self.items) > self.MAX_ITEMS: count = len(self.items) - self.MAX_ITEMS del self.items[-count:] def order_items(self, index): ''' Move the selected item tot he top of the list. ''' # We only need to move the item to the top if it is not the first item. if index > 0: self.emit(SIGNAL('layoutAboutToBeChanged()')) value = self.items[index] del self.items[index] self.items.insert(0, value) self.emit(SIGNAL('layoutChanged()')) class MComboBox(QComboBox): def __init__(self, parent=None): QComboBox.__init__(self, parent) self.setEditable(True) # The default policy is InsertAtBottom. InsertAtTop must be set # otherwise the model will move the item to the top and the QComboBox # will select the last index. In the case of reaching max items the # index will be invalid because the removal is after the QComboBox has # stored the value of the last index. self.setInsertPolicy(QComboBox.InsertAtTop) # Without this the QComboBox will not call set data for us to check # for items already in the model if the item is a duplicate. self.setDuplicatesEnabled(True) self._model = ComboModel() self.setModel(self._model) # We need to tell the model when an item is selected so it can be moved # to the top of the history list. self.connect(self, SIGNAL('activated(int)'), self._model.order_items) def main(): app = QApplication([]) bx = MComboBox() bx.show() app.exec_() if __name__ == '__main__': main()
One thing to note is that in this example the model stores the items in a list called items. This can be replaced with some other way to retrieve the history items. For example with a connection to an SQLite DB.
* QCompleter and Comma-Separated Tags
Posted on July 4th, 2009 by John. Filed under programming.
Here is a python script demonstrating how to use QCompleter to complete multiple tags in a QLineEdit. A few features of this script are: removing tags from the drop down that already appear in the QLineEdit, caching the tags, and inserting a , after completion to ease adding more tags. There are a few parts of this script that I’m going to go into detail about.
#!/usr/bin/env python ''' Copyright (c) 2009 John Schember Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ''' import sys from PyQt4.Qt import Qt, QObject, QApplication, QLineEdit, QCompleter, \ QStringListModel, SIGNAL TAGS = ['Nature', 'buildings', 'home', 'City', 'country', 'Berlin'] class CompleterLineEdit(QLineEdit): def __init__(self, *args): QLineEdit.__init__(self, *args) QObject.connect(self, SIGNAL('textChanged(QString)'), self.text_changed) def text_changed(self, text): all_text = unicode(text) text = all_text[:self.cursorPosition()] prefix = text.split(',')[-1].strip() text_tags = [] for t in all_text.split(','): t1 = unicode(t).strip() if t1 != '': text_tags.append(t) text_tags = list(set(text_tags)) self.emit(SIGNAL('text_changed(PyQt_PyObject, PyQt_PyObject)'), text_tags, prefix) def complete_text(self, text): cursor_pos = self.cursorPosition() before_text = unicode(self.text())[:cursor_pos] after_text = unicode(self.text())[cursor_pos:] prefix_len = len(before_text.split(',')[-1].strip()) self.setText('%s%s, %s' % (before_text[:cursor_pos - prefix_len], text, after_text)) self.setCursorPosition(cursor_pos - prefix_len + len(text) + 2) class TagsCompleter(QCompleter): def __init__(self, parent, all_tags): QCompleter.__init__(self, all_tags, parent) self.all_tags = set(all_tags) def update(self, text_tags, completion_prefix): tags = list(self.all_tags.difference(text_tags)) model = QStringListModel(tags, self) self.setModel(model) self.setCompletionPrefix(completion_prefix) if completion_prefix.strip() != '': self.complete() def main(): app = QApplication(sys.argv) editor = CompleterLineEdit() completer = TagsCompleter(editor, TAGS) completer.setCaseSensitivity(Qt.CaseInsensitive) QObject.connect(editor, SIGNAL('text_changed(PyQt_PyObject, PyQt_PyObject)'), completer.update) QObject.connect(completer, SIGNAL('activated(QString)'), editor.complete_text) completer.setWidget(editor) editor.show() return app.exec_() if __name__ == '__main__': main()
Looking at the main() function the editor widget’s text_changed signal is connected to the completer’s update slot. This serves two purposes. It provides the completer with all tags that are in the editor. Also, it provides the completer with the prefix of the current text that is being entered. The prefix is used for listing matching tags that are stored in the completer’s cache.
editor’s complete_text function takes the text in the editor before the cursor and subtracts the length of the prefix from that position because the prefix will be included in the completed text. The text before, the completed text, a comma, a space, and the text after the cursor are combined. This becomes the text in the QLineEdit. The cursor is advanced the position it was at minus the length of the prefix and plus 2 characters (, ) so that typing can immediately continue.
TAGS can be replaced with a function that gets all relevant tags if caching is not wanted.
Also, note that completer.setWidget(editor) was used not QLineEdit’s setCompleter() function. If setCompleter is used completion will only take place at the beginning of the QLineEdit. Meaning it will match everything before as one string and ignore the , delimiter separating the tags.
* Calibre Week in Review
Posted on May 31st, 2009 by John. Filed under calibre.
Not much happened this week. A few bug fixes and a new output format, RTF. It produces acceptable results. It also embeds images into the file. The output could use some tweaking, but this will come with time. The only caveat is the output is ascii only. This is to keep compatibility with Cailbre’s RTF intput which can only accept ascii rtf files.
Pluginize has been merged back into trunk. Once a bit of testing is done by Kovid, he will be rolling out a beta for the 0.6 release. For those of you, like me, who use Ubuntu and build Calibre from source, there is a little change you will need to make in order to have it build. Open the file /usr/lib/python2.6/dist-packages/PyQt4/uic/Compiler/qtproxies.py and modify _qwidgets on line 238 to include “QWizardPage”.
* Building the eBook Tools
Posted on December 23rd, 2008 by John. Filed under programming.
It’s come to my attention that while I’ve posted a few eBook formating tools I wrote and use I never posted how to build them. Since I’m using Qt the easiest way to build them is to use qmake and make.
The build process is simple. Create a pro file for the project say fix_end_ebook_txt.pro. Run qmake then run make. You will end up with an executable. Just remember that this requires Qt, make, and a C++ complier (g++ on *nix or mingw on Windows).
fix_end_ebook_txt.pro
SOURCES += fix_end_ebook_txt.cpp CONFIG += qt TARGET = fix_end_ebook_txt
The above pro is very minimal and can be further tuned for the specific project but at the very least it shows how to build the Qt eBook tools I’ve posted.
* eBook Adding Empty Lines At End of File
Posted on December 22nd, 2008 by John. Filed under programming.
Continuing my work to clean up my eBooks I’ve written another little tool to help. I like for my eBooks to have two blank lines at the end of the file.
The only major caveat of this one is it assumes Unix end of lines. Meaning a single \n character. In order for this to work correctly use of the dos2unix tool is necessary for files that use a different new line format.
fix_end_ebook_txt.cpp
/* Copyright (c) 2008 John Schember Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* Ensures that there are 3 newline characters at the end of the file (two blank lines after the last of the text). This assumes Unix \n line characters. Please use dos2unix before running to ensure that the end of line characters are correct. */ #include <QFile> #include <QString> #include <QTextStream> int main(int argc, char **argv) { // Stream to write errors to the console. QTextStream errStream(stderr); // Store for the contents of the ebook. QString content; // We need an ebook file to work on. if (argc != 2) { errStream << QObject::tr("Error: No input file") << endl; return 1; } QFile ebook(argv[1]); if (!ebook.open(QIODevice::ReadWrite | QIODevice::Text)) { errStream << QObject::tr("Error: Could not open") << endl; return 1; } // We use a QTextStream to actually work on the file. QTextStream ioStream(&ebook); // We want to see what the last 3 characters are at the end of the file. ioStream.seek(ebook.size() - 3); content = ioStream.read(3); // Move to the end of the file because we want to add newlines (\n's) to // the end. ioStream.seek(ebook.size()); // We want 3 newline (\n) characters at the end of the file. Add them until // they total 3. for (int i = 0; i < (3 - content.count("\n")); i++) { ioStream << "\n" << flush; } ebook.close(); return 0; }
* eBook Paragraph Formating
Posted on December 21st, 2008 by John. Filed under programming.
Today I wrote two simple programs to help me clean up my ebooks. I prefer to keep my ebook collection as plain text files with paragraphs separated by a blank line. The first program reflows the paragraphs to put each on a single line. The second removes extraneous whitespace from the file.
The reflow is the more intensive of the two. I ran it on the largest ebook I have, Project Gutenberg’s War and Peace by Leo Tolstoy. The file is 3.1 MB.
Time to run: 7m35.494s.
Memory usage: 13.1 MB according to gnome-system-monitor.
Right now I’m loading the entire book into memory and using QStrings to work on it. Memory usage is about 4.5 x the size of the book. Thankfully plain text ebooks are fairly small. Later I’m going to look into optimizing it for size and hopefully speed.
Without further ado here are the two. They are MIT licensed and use the Qt tool kit.
fix_paragraphs_ebook_txt.cpp
/* Copyright (c) 2008 John Schember Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* Reflows txt file ebook paragraphs. Paragraphs should be separated by a blank line. Takes paragraphs that have hard breaks and puts all lines onto a single line. For Example: INPUT This is a multi line paragraph. It comprises a few lines but has hard breaks. Now for the second borken apart paragraph. OUTPUT This is a multi line paragraph. It comprises a few lines but has hard breaks. Now for the second broken apart paragraph. */ #include <QFile> #include <QRegExp> #include <QString> #include <QTextStream> int main(int argc, char** argv) { // Stream to write errors to the console. QTextStream errStream(stderr); // Regular expression to search for broken paragraphs. Works by looking // for char newline char. A proper ebook should have paragraphs separated // by a blank line meaning char newline newline char. QRegExp re("[^\n]\n[^\n]"); // Store for the contents of the ebook. QString content; // We need an ebook file to work on. if (argc != 2) { errStream << QObject::tr("Error: No input file") << endl; return 1; } QFile ebook(argv[1]); if (!ebook.open(QIODevice::ReadWrite | QIODevice::Text)) { errStream << QObject::tr("Error: Could not open") << endl; return 1; } // We use a QTextStream to actually work on the file. QTextStream ioStream(&ebook); // Read the entire file contents into memory. content = ioStream.readAll(); while (content.contains(re)) { // Remove the newline when there is a match with the regular expression. content = content.replace(content.indexOf(re)+1, 1, " "); } // Truncate the ebook so we don't end up with the original contents after // our modified contents. if (!ebook.resize(0)) { errStream << QObject::tr("Error: Could not truncate file") << endl; return 1; } // Store the modified content back on disk. ioStream.seek(0); ioStream << content; ebook.close(); return 0; }
remove_extra_whitespace_ebook_txt.cpp
/* Copyright (c) 2008 John Schember Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* Removes extraneous whitespace in a txt file ebook. This will remove every '\t', '\v', '\f', '\r', and will replace multiple occurrences ' ' with a single one. For Example: INPUT This is a bad line. Now for the second borken line. OUTPUT This is a bad line. Now for the second borken line. */ #include #include #include int main(int argc, char **argv) { // Stream to write errors to the console. QTextStream errStream(stderr); // Store for the contents of the ebook. QString content; // We need an ebook file to work on. if (argc != 2) { errStream << QObject::tr("Error: No input file") << endl; return 1; } QFile ebook(argv[1]); if (!ebook.open(QIODevice::ReadWrite | QIODevice::Text)) { errStream << QObject::tr("Error: Could not open") << endl; return 1; } // We use a QTextStream to actually work on the file. QTextStream ioStream(&ebook); // Read every line and remove the extras we don't want. while (!ioStream.atEnd()) { content += ioStream.readLine().simplified() + "\n"; } // Truncate the ebook so we don't end up with the original contents after // our modified contents. if (!ebook.resize(0)) { errStream << QObject::tr("Error: Could not truncate file") << endl; return 1; } // Store the modified content back on disk. ioStream.seek(0); ioStream << content; ebook.close(); return 0; }
Tags
Archives
- February 2012 (1)
- January 2012 (3)
- December 2011 (2)
- November 2011 (1)
- October 2011 (3)
- September 2011 (9)
- August 2011 (15)
- July 2011 (5)
- June 2011 (3)
- May 2011 (4)
- April 2011 (2)
- March 2011 (2)
- February 2011 (4)
- January 2011 (4)
- December 2010 (2)
- November 2010 (1)
- October 2010 (1)
- August 2010 (3)
- July 2010 (4)
- June 2010 (1)
- May 2010 (2)
- March 2010 (1)
- January 2010 (8)
- December 2009 (5)
- November 2009 (6)
- October 2009 (4)
- September 2009 (2)
- August 2009 (6)
- July 2009 (6)
- June 2009 (4)
- May 2009 (6)
- April 2009 (4)
- March 2009 (2)
- February 2009 (4)
- January 2009 (4)
- December 2008 (7)
- November 2008 (2)