Subclassing QAbstractProxyModel, adding tree nodes that do not exist in the source model - c++

I am trying to implement a ProxyModel that takes a source Model that represents a flat, table like structure.
Then you can select one column of this model as a grouping value.
The proxy model should then create as many top nodes as there are distinct values of this column and sort the underlying rows into them.
But when doing this, is subclassing QAbstractProxyModel a viable option?
Since mapToSource(const QModelIndex& proxyIndex) will not always return a valid source index for a valid proxy index.
Does someone have a better solution, perhaps just using a QAbstractItemModel without the built in proxy functionality?

Here are two example projects which address this issue, for anyone's future reference. (One of them is mine, I do not mean to spam, it just seems relevant.)
GroupedItemsProxyModel (doc)
QGroupingProxyModel (doc)
Edit (response to comment): Both projects implement what, I believe, the question is asking about. I think examining the source provides the best examples and would obviously be too long to paste it all here. I've provided links directly to the source and to relevant documentation.

UPDATE: Sorry, understood this a little wrong. I never derived from abstract proxy model so I am not sure about that. However I would start using a QSortFilterProxyModel. It may happen that it has some functionality you do not need but this does not hurt. On the other hand implementing your on proxy model could hurt since it requires additional work and know-how.
If you should experience problems switching from the non-grouped structure to the grouped structure within the proxy model consider switching the model of the view (one showing grouped data, one showing original un-grouped data).

Related

How to extend the event/occurrence models in django-scheduler

I'd like to augment events/occurrences in django-scheduler with three things:
Location
Invitees
RSVPs
For Location, my initial thought was to subclass Event and add Location as a foreign key to a Location class, but my assumption is that each occurrence saved won't then include Location, so if the location changes for one occurrence, I'll have nowhere to store that information.
In this situation, is it recommended to create an EventRelation instead? Will I then be able to specify a different Location for one occurrence in a series? The EventRelation solution seems untidy to me, I'd prefer to keep models in classes for clarity and simplicity.
I think Invitees is the same problem, so presumably I should use a similar solution?
For RSVPs, I intend to make an RSVP class with Occurrence as a foreign key, and as far as I can tell that should work without any issues as long as I save the occurrence before attaching it to an RSVP?
I've read all the docs, all the GitHub issues, various StackOverflow threads, the tests, the model source, etc, but it's still unclear what the "right" way to do it is.
I found a PR which introduces abstract models: https://github.com/llazzaro/django-scheduler/pull/389 which looks like exactly what I want, but I'm reluctant to use code which was seemingly abandoned 18 months ago as I won't get the benefit of future improvements.
EDIT: I'm now thinking that another way to do this would be to have just one object linked to the event using EventRelation, so I'd have an "EventDetails" object connected to the Event via EventRelation, then include FKs to Location, Guests, etc from that object.
I should then also be able to subclass my EventDetails object with different kinds of events and attach those too. I'll give it a go ant see if it works!
Just in case anyone find this and is wondering the same thing: I ended up ditching Django-scheduler and using Django-recurrence instead. Had to do a bit more work myself, but it was easier to create the custom event types that I was looking for. Worked pretty well!

wxDataViewModel: What is it and how do i use it?

i read the documentation http://docs.wxwidgets.org/3.0/classwx_data_view_model.html several times but it hardly answers any questions. Maybe i'm confused as to the function of this class?
so i riddle you this:
Is this a View Model as we know from MVVM?
How do you implement a derivative?
How do you set data in the containing wxDataViewListCtrl?
Is this the right/recommended way to make a table?
As its name subtly hints, wxDataViewModel is indeed the model in the usual MVC design (while wxDataViewCtrl is both the view and the controller).
You can see a couple of examples of custom models in the dataview sample.
Notice that wxDataViewListCtrl is mostly a compatibility class made for transitioning the code using wxListCtrl to wxDataViewCtrl and it already defines its own trivial list model. I don't recommend using it unless this is exactly what you need.

Qt custom delegates

I have custom model with different data types like string (file path) or double which should be edited using combobox with a few items.
It is not clear for me why delegates should be applied to views and not to models...
So, should I pass some kind of flag from my model and then use different delegates for those items according to those flags?
What is the best practice to make it?
EDIT: I'll try to clarify my question:
I have model with the map (key - value (structure that can contain different types like QVariant)) and it's necessary to set different delegates for each row of my, say, QTableView.
What is the best way to pass some "flag" for every item from my model and then handle this flag to set appropriate delegate for the given row?
EDIT2:
This model-view pair is for storage and editing software options with different types.
From the docs:
Unlike the Model-View-Controller pattern, the model/view design does not include a completely separate component for managing interaction with the user.
Delegates are supposed to tackle the "how" in "how should users interact with my data" (that's why I highlighted "interaction").
For your case, that very "double" field you provided, depending on it's interpretation, we could use a line edit (eg exact toleration), spinbox, or even some sort of color select (interpret the value as a color). Even more, one could use a line edit with some sort of color scale for the widget to make it more clear what consequence that value may have.
Correct way? They're tools, not one better than other but rather "one to tackle a specific problem". Can't tell what's the correct way from the info provided.
I suggest re-asking the question with much more info if you still have doubts.

How to save relations of same class?

I had problem with saving relation to object with same class as parent.
You can check this problem here.
When I read that I can easily set the relationship after the promise has fulfilled here I created another example with that info in mind. But it doesn't work as I expect.
What I expect
Create array of Box instances with relation to previous Box instance in each.
And the question is if I'm doing something wrong or it's a bug. Let me know if you need any informations.
Your example isn't clear and simple enough. It needs to be isolated to EXACTLY what you're having an issue about and nothing else.
Having said that, I have had quite a bit of success saving relations to objects with the same class as parent, and so I don't think this is a problem with Ember Data or Ember.
Your code is quite convoluted and uses the sync library, which I'm not faimilar with.
It's a good idea to things as simply as possible at first, so try creating a jsbin with just the isolated functionality relating to saving relations that you're attempting, and then adding additional layers of functionality and testing after each add.

Django: How to implement system flags

I am developing an application in Django and I am curious on how I can go about adding a model such that only 1 row is only ever present (i.e. Singleton).
As an example, I'd like to maintain a set of boolean flags of the application i'm running as to whether: it's on or off (so I can manually turn it on or off, perhaps even per module).
I can't see any part of the docs explaining a good way to go about setting this up.
Any suggestions?
Not sure from you explanation in what context you require this but I have a model which holds a number of key/value pairs used in validator checks and other things. The keys are all needed by each implementation of the project but the values will differ between projects. The values should be maintainable by an admin user. The values usually do not need to change very much once set. Given that, I decided to put them in a model. It is a bit weird but simple enough.
You should be able to limit write access to the model to the one row for either your app or your users through your code.
only ever reference the first row in the QuerySet
row = MyVariables.objects.all()[0]
Test if there are rows first. if you think there might accidentally be more than one record then make sure it is ordered (but that should never happen if you did (1) correctly.
There are a couple of apps already dealing with this, check out http://djangopackages.com/grids/g/live-setting/
I'm also a bit confused on your goal, but I'd recommend looking at the Model Instance section of the docs. You should probably look at customizing the validation or cleaning of the model.
If your goal is to only have 1 row flagged in the table for your model: during the validation you can run a query to see if any other row is flagged, and update them to be not flagged. (or delete them).
This question Unique BooleanField value in Django may be helpful.