Clone QTextDocument Subclass - qt

I have a subclass of QTextDocument that overrides QTextDocument::loadResource(int type, const QUrl &name).
I want to clone it.
Ideas ?

You can't use QTextDocument::clone for that. Neither can you reimplement it because it's not virtual. You should create another clone method (you can name it clone but I'd give it another name to avoid confusion) and implement cloning yourself. You can use toHtml and setHtml to copy content from one object to another:
MyTextDocument* MyTextDocument::my_clone(QObject * parent = 0) {
MyTextDocument* other = new MyTextDocument(parent);
other->setHtml(toHtml());
return other;
}
There are however many other properties that should be copied. See how QTextDocument::clone method is implemented:
QTextDocument *QTextDocument::clone(QObject *parent) const
{
Q_D(const QTextDocument);
QTextDocument *doc = new QTextDocument(parent);
QTextCursor(doc).insertFragment(QTextDocumentFragment(this));
doc->rootFrame()->setFrameFormat(rootFrame()->frameFormat());
QTextDocumentPrivate *priv = doc->d_func();
priv->title = d->title;
priv->url = d->url;
priv->pageSize = d->pageSize;
priv->indentWidth = d->indentWidth;
priv->defaultTextOption = d->defaultTextOption;
priv->setDefaultFont(d->defaultFont());
priv->resources = d->resources;
priv->cachedResources.clear();
#ifndef QT_NO_CSSPARSER
priv->defaultStyleSheet = d->defaultStyleSheet;
priv->parsedDefaultStyleSheet = d->parsedDefaultStyleSheet;
#endif
return doc;
}
If these properties are important to you, you need to copy them manually in your my_clone implementation. You can're use QTextDocumentPrivate class because it's internal. So you can't just copy default implementation. But there is a normal way to set each of listed properties in the API.
Be aware of forward compability issues. If new properties appeared in newer Qt versions, your implementation will not copy them in opposite to default clone implementation. That could be a problem so this approach is not perfect.

Related

Change font of contents in QTextEdit from my application resource

I have Qt Resource file (res.qrc) in my Qt Application. I imported my custom font in resource as below:
:/fonts/aa_marcus_east_syriac.ttf
Also i define in header file:
private:
QFont assyrianEventsAAMarcusEastSyriac;
I used QTextEdit in mainwindow. When user click on a button, my application read a text file. every row in text file should be imported in QTextEdit but some lines should be has aa_marcus_east_syriac.ttf font from my resource. So I wrote this codes:
void Widget::readMonthAssyrianEvents()
{
QStringList eventsList;
eventsList = readEventFile();
ui->notificationTextEdit->setCurrentFont(assyrianEventsAAMarcusEastSyriac);
for (int index = 0; index < eventsList.length(); index++)
{
QString eventType, eventContent;
QStringList tempStringList = eventsList[index].split('|');
eventType = tempStringList[0];
eventContent = tempStringList[1];
if (eventType == "0")
ui->notificationTextEdit->append(eventContent);
}
}
readEventFile() function works fine. It read text file and get all lines as QStringList. the "assyrianEventsAAMarcusEastSyriac" variable initialized in another function called init(). this is init() function:
int id = QFontDatabase::addApplicationFont(":/fonts/aa_marcus_east_syriac.ttf");
QString family = QFontDatabase::applicationFontFamilies(id).at(0);
assyrianEventsAAMarcusEastSyriac.setFamily(family);
assyrianEventsAAMarcusEastSyriac.setPointSize(20);
My problem is that QTextEdit doesn't change font of it's contents to my custom font.
How can I solve this problem? Please help me guys.
Thanks
I think the error is in
QString family = QFontDatabase::applicationFontFamilies(id).at(0);
Have you checked that .at(0) is actually your custom font ?
Most likely you can solve this by calling assyrianEventsAAMarcusEastSyriac.setFamily
with an explicit string of the font family just like
assyrianEventsAAMarcusEastSyriac.setFamily("Marcus East Syriac");
If that doesn't work either, maybe your custom font is malformed or doesn't provide a font family name. Therefore I suggest you to try first with a working font and then eventually go back to custom stuff.

Qt Installer framework - Customizing the UNINSTALLER

I am not able to get a hold of customizing the un-installation process, though I could succeed to some extent customizing the installation process. When I register for the signals “installationFinished” my functions in the installscript.qs gets called, but surprisingly the signals “uninstallationStarted and uninstallationFinished” never gets called if I register some functions. Not sure why? Also in qinstaller.h we have the below code for identifying different state/stages of installation, but how can I get similar information for un-installation?
enum Status {
InstallerUnfinished,
InstallerCanceledByUser,
InstallerRunning,
InstallerFailed,
InstallerSucceeded,
};
int status() const;
enum WizardPage {
Introduction = 0x1000,
TargetDirectory = 0x2000,
ComponentSelection = 0x3000,
LicenseCheck = 0x4000,
StartMenuSelection = 0x5000,
ReadyForInstallation = 0x6000,
PerformInstallation = 0x7000,
InstallationFinished = 0x8000,
End = 0xffff
};
Did you try this?
function Controller()
{
installer.uninstallationStarted.connect(this, Controller.prototype.runMyFunc);
}
Controller.prototype.runMyFunc = function()
{
// your code here
}

Checking then Adding items to QCompleter model

I am currently working on a code editor written in Qt,
I have managed to implement most of the features which I desire, i.e. auto completion and syntax highlighting but there is one problem which I can't figure out.
I have created a model for which the QCompleter uses, which is fine for things like html tags and c++ keywords such as if else etc.
But I would like to add variables to the completer as they are entered by the user.
So I created an event on the QTextEdit which will get the word (I know I need to check to make sure that it is a variable etc but I just want to get it working for now).
void TextEdit::checkWord()
{
//going to get the previous word and try to do something with it
QTextCursor tc = textCursor();
tc.movePosition(QTextCursor::PreviousWord);
tc.select(QTextCursor::WordUnderCursor);
QString word = tc.selectedText();
//check to see it is in the model
}
But now I want to work out how to check to see if that word is already in the QCompleters model and if it isn't how do I add it?
I have tried the following:
QAbstractItemModel *m = completer->model();
//dont know what to do with it now :(
You can check if word is in your QCompleter really by using
QAbstractItemModel *m = completer->model();
as you can see, method model() returns const pointer.
That is good for checking procedure, you can check like this:
bool matched = false;
QString etalon("second");
QStringListModel *strModel = qobject_cast<QStringListModel*>(completer.model());
if (strModel!=NULL)
foreach (QString str, strModel->stringList()) {
if (str == etalon)
{
matched = true;
break;
}
}
qDebug()<<matched;
But for your purposes, I recommend you to declare QStringListModel, and connect it to your completer, and then, all of operations you'll must do thru your model, according to Qt's principles of MVC programming (http://doc.qt.digia.com/qt/model-view-programming.html).
Your code can be like this:
// declaration
QCompleter completer;
QStringListModel completerModel;
// initialization
completer.setModel(&completerModel);
QStringList stringListForCompleter;
stringListForCompleter << "first" << "second" << "third";
completerModel.setStringList(stringListForCompleter);
// adding new word to your completer list
completerModel.setStringList(completerModel.stringList() << "New Word");
Good luck!

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor) and another like this=>InsertJCDC(fakeBox).
When I stub out the parent I want to return fakePor. But when I run the code it returns null instead. Here is the unit test.
[Test]
public void PorBLL_InsertByPorInsertCV_DoingGoodCase()
{
// Startup object mapper
_Bootstrapper.Bootstrap();
// create the mock for generic Crud
IGenericCrud mockGenericCrud = MockRepository.GenerateMock<IGenericCrud>();
PorInsertCV fakePor = new PorInsertCV();
PorBoxInsertCV fakeBox = new PorBoxInsertCV();
// build fake return
PorEO fakePorNewRow = new PorEO();
fakePorNewRow.PorId = 22;
// stub parent and child insert routines.
mockGenericCrud.Stub(c => c.InsertJCDC<PorEO, PorInsertCV>(fakePor)).Return(fakePorNewRow);
mockGenericCrud.Stub(c => c.InsertJCDC<PorBoxEO, PorBoxInsertCV>(fakeBox)).Return(null);
ObjectFactory.Inject(typeof(IGenericCrud), mockGenericCrud);
IPorBLL localWithMock = ObjectFactory.GetInstance<IPorBLL>();
// build user args to csll bll with and use for insert
PorInsertCV userArgs = new PorInsertCV();
userArgs.AccessionNbr = "364-80-0007";
userArgs.NbrBoxes = 11;
userArgs.RegId = 20;
userArgs.TransmitedDt = Convert.ToDateTime("1/30/1980");
// call the bll using the stub
localWithMock.InsertByPorInsertCV(userArgs);
}
Any help is greatly appreciated
I can't really follow your code that well, but I'll give it a shot.
From my skills of deduction, this line here is the one giving you issues:
mockGenericCrud.Stub(c => c.InsertJCDC<PorEO, PorInsertCV>(fakePor)).Return(fakePorNewRow);
Because you're expecting fakePorNewRow to be returned when you call localWithMock.InsertByPorInsertCV(userArgs); - yeah?
If that's your case, what your problem is, is that it will only return fakePorNewRow when it is given fakePor ... not userArgs as you have given it.
Tell me if I'm completely off track.
HTHs,
Charles
Ps. You might want to add the tag of which mocking framework you are using to the question.

Is there a API from Flex/Flash to list all the UIComponent?

I am trying to create an API utility using Adobe AIR where given a component name it should list all it methods, variables, and other properties of that component.
But I could not find an API to list all the UIComponents of Flex/Flash.
is there an API to lookup all the UIComponent or if there is no such API, can anyone tell me how do i lookup the components?
Note: I have tried using flash.utils.describeType() but it is not able to suit my requirement as the input to the method is passed dynamically using TextInput.
~Jegan
It sounds like you are asking two things:
How to list all of the properties/methods of a UIComponent.
How to list all of the UIComponents in the Flex SDK.
The first one can be done with describeType(). Given that the method is passed dynamically using TextInput, you need to just include all of the UIComponents into the SWF. You can do that with the following include statement for each component in the SDK (this example is with the Button):
include mx.controls.Button; Button;.
Then you can do something like this:
var className:String = myTextArea.text; // mx.controls::Button or mx.controls.Button
var clazz:Class = flash.utils.getDefinitionByName(className);
var methods:Array = getMethods(clazz);
var properties:Array = getProperties(clazz); // write a similar method for accessors and variables
/**
* Returns a list of methods for the class.
* Pass in the superclass depth for how many classes
* you want this to return (class tree). If it's -1, it will return the whole method list
*/
public static function getMethods(target:Object, superclassDepth:int = -1, keepGeneratedMethods:Boolean = false):Array
{
var description:XML = DescribeTypeCache.describeType(target).typeDescription;
var hierarchy:Array = getHierarchy(target);
var methodList:XMLList = description..method;
methodList += description.factory..method; // factory is required if target is Class
var methodName:String
var methods:Array = [];
var declaredBy:String;
var methodXML:XML;
var i:int = 0;
var n:int = methodList.length();
for (i; i < n; i++)
{
methodXML = methodList[i];
declaredBy = methodXML.#declaredBy;
methodName = methodXML.#name;
// we break because the xml list is orded hierarchically by class!
if (superclassDepth != -1 && hierarchy.indexOf(declaredBy) >= superclassDepth)
break;
// ignore methods that start with underscore:
if (methodName.charAt(0) == "_" && !keepGeneratedMethods)
continue;
methods.push(methodName);
}
// sort the method list, so there's some kind of order to the report:
methods.sort(Array.CASEINSENSITIVE);
return methods;
}
The second one, listing the UIComponents in the SDK, is a bit more complex because there's no stable list of components in the SDK (depends on version, etc.). What you can do, however, is get a list of every class included in the swf. You do this by generating a Link Report (which is used for helping out modules), which is just a compiler argument:
-link-report=my_report.xml
Then you can figure out a way to find the unique xml nodes in that file, and which ones extend UIComponent. I use ruby and the nokogiri xml parser for ruby to do that. There is also a neat AIR app to visualize the Link Report.
If you're doing the first case, that method should get you started on listing all properties/methods of a UIComponent. If you're doing the second, that will give you a list of all UIComponents included in the swf.
Let me know if that helps,
Lance
Do you need to do anything with the data, other than displaying it? If not, it might be easiest to pull up the right page from Adobe's online API docs.

Resources