How can I listen for the deletion of a ProjectItem via DTE? - visual-studio

I've got a designer that relies on the existence of other solution items. If one of those items is deleted the designer crashes and you have to edit as XML to fix. Not exactly user friendly.
I do, however, have the DTE object representing the instance of Visual Studio, as well as the ProjectItems I am dependent on.
Is it possible to, somewhere in the depths of the DTE, register a listener for the deletion of that ProjectItem? And, if so, How would I do it?

It looks like the culprit here is garbage collection. I found the following two event sets behaved identically.
Events2 events2 = dte.Events as Events2;
if (events2 != null)
{
this.projectItemsEvents = events2.ProjectItemsEvents;
this.projectItemsEvents.ItemAdded += this.ProjectItemsEvents_ItemAdded;
this.projectItemsEvents.ItemRemoved += this.ProjectItemsEvents_ItemRemoved;
this.projectItemsEvents.ItemRenamed += this.ProjectItemsEvents_ItemRenamed;
}
this.csharpProjectItemsEvents =
dte.Events.GetObject("CSharpProjectItemsEvents") as ProjectItemsEvents;
if (this.csharpProjectItemsEvents != null)
{
this.csharpProjectItemsEvents.ItemAdded += this.CSharpProjectItemsEvents_ItemAdded;
this.csharpProjectItemsEvents.ItemRemoved += this.CSharpProjectItemsEvents_ItemRemoved;
this.csharpProjectItemsEvents.ItemRenamed += this.CSharpProjectItemsEvents_ItemRenamed;
}
The key to both was making sure to keep a reference to the events object in the subscriber. Once I added the reference, they behaved like I expected.
private ProjectItemsEvents projectItemsEvents;
private ProjectItemsEvents csharpProjectItemsEvents;

Check out this FAQ article which explains how to register for ProjectItems events (including ItemDeleted).

Related

signature and functionality of selection callback of treeview in gtkmm

I have a treeview and want to get notified if the selection changes. What is the signature for the callback?
I found a code snippet like:
Gtk::TreeView *treeview = Gtk::manage(new Gtk::TreeView);
Glib::RefPtr< Gtk::TreeSelection > sel = treeview->get_selection();
sel->set_mode( Gtk::SELECTION_MULTIPLE );
sel->set_select_function(sigc::ptr_fun(&SelFun));
But I can't find anything about the SelFun!
How is the signature
How to find out which rows and columns are selected inside this function?
How to access data from the model with that object
Yes, I have actually no idea how the TreeView/Model/Path/Selection interacts. Every link to an example is highly welcome!
You seem to want multiple selection. I had the same problem too. Once you have enabled mutliple selection, getting the selected rows is a little more difficult. The method of acquiring them varies slightly.
I'll provide the most general method. First, you need to overload the signal_changed() signal after you have enabled multiple selection. Then, assign the TreeView's TreeSelection to a RefPtr for easy access.
Glib::RefPtr<Gtk::TreeSelection> TreeView_TreeSelection;
TreeView_TreeSelection = your_TreeView.get_selection();
Next, connect the TreeSelection to the signal_changed() signal.
TreeView_TreeSelection -> signal_changed().connect(sigc::mem_fun(your_TreeView,
&your_Class::on_selection_changed));
Now, make sure to make a void function header in "your_Class" named on_selction_changed() or whatever you want. Just make sure to change the name in the connection above to whatever your class' name is.
The final step is to make the function. Here is a simple example of getting a vector of all of the TreePaths of the rows selected, then converting those TreePaths into a vector of TreeModel::Row pointers.
void your_Class::on_selection_changed()
{
if((TreeView_TreeSelection -> count_selected_rows()) == 0)
{
return;
}
vector<Gtk::TreeModel::Path> selected_rows = TreeView_TreeSelection -> get_selected_rows();
vector<Gtk::TreeModel::Row*> selected_TreeRows;
vector<Gtk::TreeModel::Path>::iterator TreePath_iterator = selected_rows.begin();
Gtk::TreeRow *row;
while(TreePath_iterator != selected_rows.end()
{
selected_row_it = p_TreeModel -> get_iter(TreePath_iterator);
row = (*selected_row_it);
selected_TreeRows.push_back(row);
TreePath_iterator++;
}
}
Do you know how to iterate through a TreeModel using the STL-like contain API called children() of a TreeModel? It's most useful for iterating over all of the rows of a TreeModel or getting the size (AKA row count) of a TreeModel. Its use depends on whether you're using a ListStore, TreeStore or a custom TreeModel.

Unit Test Only Passes in Debug Mode, Fails in Run Mode

I have the following UnitTest:
[TestMethod]
public void NewGamesHaveDifferentSecretCodesTothePreviousGame()
{
var theGame = new BullsAndCows();
List<int> firstCode = new List<int>(theGame.SecretCode);
theGame.NewGame();
List<int> secondCode = new List<int>(theGame.SecretCode);
theGame.NewGame();
List<int> thirdCode = new List<int>(theGame.SecretCode);
CollectionAssert.AreNotEqual(firstCode, secondCode);
CollectionAssert.AreNotEqual(secondCode, thirdCode);
}
When I run it in Debug mode, my code passes the test, but when I run the test as normal (run mode) it does not pass. The exception thrown is:
CollectionAssert.AreNotEqual failed. (Both collection contain same elements).
Here is my code:
// constructor
public BullsAndCows()
{
Gueses = new List<Guess>();
SecretCode = generateRequiredSecretCode();
previousCodes = new Dictionary<int, List<int>>();
}
public void NewGame()
{
var theCode = generateRequiredSecretCode();
if (previousCodes.Count != 0)
{
if(!isPreviouslySeen(theCode))
{
SecretCode = theCode;
previousCodes.Add(previousCodes.Last().Key + 1, SecretCode);
}
}
else
{
SecretCode = theCode;
previousCodes.Add(0, theCode);
}
}
previousCodes is a property on the class, and its Data type is Dictionary key integer, value List of integers. SecretCode is also a property on the class, and its Data type is a List of integers
If I were to make a guess, I would say the reason is the NewGame() method is called again, whilst the first call hasn't really finished what it needs to do. As you can see, there are other methods being called from within the NewGame() method (e.g. generateRequiredSecretCode()).
When running in Debug mode, the slow pace of my pressing F10 gives sufficient time for processes to end.
But I am not really sure how to fix that, assuming I am right in my identification of the cause.
What happens to SecretCode when generateRequiredSecretCode generates a duplicate? It appears to be unhandled.
One possibility is that you are getting a duplicate, so SecretCode remain the same as its previous value. How does the generator work?
Also, you didn't show how the BullsAndCows constructor is initializing SecretCode? Is it calling NewGame?
I doubt the speed of keypresses has anything to do with it, since your test method calls the functions in turn without waiting for input. And unless generateReq... is spawning a thread, it will complete whatever it is doing before it returns.
--after update--
I see 2 bugs.
1) The very first SecretCode generated in the constructor is not added to the list of previousCodes. So the duplicate checking won't catch if the 2nd game has the same code.
2) after previousCodes is populated, you don't handle the case where you generate a duplicate. a duplicate is previouslySeen, so you don't add it to the previousCodes list, but you don't update SecretCode either, so it keeps the old value.
I'm not exactly sure why this is only showing up in release mode - but it could be a difference in the way debug mode handles the random number generator. See How to randomize in WPF. Release mode is faster, so it uses the same timestamp as seed, so it does in fact generate exactly the same sequence of digits.
If that's the case, you can fix it by making random a class property instead of creating a new one for each call to generator.

In C++/CLI how do you define thread-safe event accessors?

The code sample "How to: Define Event Accessor Methods" at
http://msdn.microsoft.com/en-us/library/dw1dtw0d.aspx
appears to mutate the internal pE without taking locks. (It doesn't look like Delegate::Combine does anything magical that would prevent issues.) It also does
void raise() {
if (pE != nullptr)
pE->Invoke();
}
which can be problematic if pE changes to null between the check and the Invoke(). I have two questions:
Am I right in that the existing code is not thread-safe?
Since I want a thread-safe version of the code, I was thinking of locking the add and remove functions. Is it premature optimization to use
void raise() {
MyDel^ handler = pE;
if (handler != nullptr)
handler->Invoke();
}
or should I just lock that function too?
All three accessors are thread-safe by default (raise includes a null-check, and uses a local variable to avoid the race condition) unlike the example in the page you linked.
When it comes to custom event implementations, you're right about needing to synchronize the add and remove accessors. Just put a mutex around the implementation. But there's no need to throw away type safety by calling Delegate::Combine and then casting, since operator + and - are overloaded for delegate handles. Or you can go lockless, as follows:
void add(MyDel^ p)
{
MyDel^ old;
MyDel^ new;
do {
old = pE;
new = pE + p;
} while (old != Interlocked::CompareExchange(pE, new, old));
}
Define remove mutatis mutandis (new = pE - p;). And the code you gave for raise will be perfectly fine for a custom event implementation.
In summary, that MSDN sample is total garbage. And the simplest way to achieve thread-safety is with an auto-implemented event.

Using QtPropertyBrowser as advanced configuration editor

Has anyone used QtPropertyBrowser as an advanced configuration editor? All I see a GUI examples, to edit properties of GUI elements. But how would I start if I wanted to edit something more abstract, like application configuration.
Here's an example:
I'm creating an application which has a multipage configuration dialog. Some settings are about the printer, some are about a database, some are generic application settings, some are about template filenames and/or templates. But before designing all those detailed config pages in the editor, I'd like to have an "advanced" or "expert" tab in the dialog which lists all imaginable configuration options. Later in the design process I would collect more and more options, decide which are the "easy" options to put into some more user-friendly configuration pages. And googling around I came accross QtPropertyBrowser which seems to be the right tool. But I'm not sure how to start? I'm pretty sure that, instead of GUI objects, I need some sort of abstract configuration objects (one or more). But I don't know where or how to start with that. Currently all my ideas look way to complicated.
Any suggestions or pointers to hints?
You might want to take a look at the runtime type information available through QMetaObject class. Your data objects should be QObject's descendants and have a QOBJECT macro declared. Also you would need a simple routine which would iterate through data objects properties and create and set up corresponding editor's properties. Meta object is also providing an interface for resetting values and methods invocation. More information on the Qt property system is here: The Property System. Below is a small example on how you could do this:
Property browser and managers declaration and initialization:
QtTreePropertyBrowser *_browser;
QtIntPropertyManager *_intManager;
QtDoublePropertyManager *_doubleManager;
QtStringPropertyManager *_stringManager;
_intManager = new QtIntPropertyManager();
_doubleManager = new QtDoublePropertyManager();
_stringManager = new QtStringPropertyManager();
_browser = new QtTreePropertyBrowser(ui->centralWidget);
load properties names and values:
void loadProperties(QObject *object)
{
_browser->clear();
if (object)
{
const QMetaObject *meta = object->metaObject();
qDebug() << "class : " << meta->className();
for (int i=0; i<meta->propertyCount(); i++)
{
QMetaProperty metaProperty = meta->property(i);
QVariant value = metaProperty.read(object);
QtProperty *property = NULL;
qDebug() << "property : " << metaProperty.name() << " : " << value.toInt();
if (metaProperty.type() == QVariant::Int)
{
property = _intManager->addProperty(metaProperty.name());
_intManager->setValue(property, value.toInt());
}
else if (metaProperty.type() == QVariant::Double)
{
property = _doubleManager->addProperty(metaProperty.name());
_doubleManager->setValue(property, value.toDouble());
}
else if (metaProperty.type() == QVariant::String)
{
property = _stringManager->addProperty(metaProperty.name());
_stringManager->setValue(property, value.toString());
}
if (property)
_browser->addProperty(property);
}
}
}
In the samples folder of QtPropertyBrowser comes a demo (object_controller) that I think is exactly what you want. Although the example shows the properties of a class QWidget ObjectController receives as referenced in function setObject (QObject * object); a QObject. Just keep in mind that all you want to inspect must inherit QObject, directly or indirectly, that is the meta-object containing information about the class. You read about the properties in Qt.

Large application design (WPF/Silverlight)

Aside from the MVVM, as well as MVC patterns for the overall structure of a WPF app, how exactly do you break up the model/controller aspect of an app into subcomponents? The reason I ask is that I have no problem architecting the solution from the perspective of the patterns mentioned above, but when it comes to actually writing the backend; I feel that i'm fudging a lot of it. I end up with high quality apps from the user perspective, but my design asthetics don't allow me accept this.
To clarify; a lot of my business logic cannot be refactored into a class (or class hierarchy, with all associated interfaces) in any easy or meaningful way without having to change the entire app. I've been developing professionally for a year and a half now, so it may be an issue of inexperience; but I feel that it's still no excuse. Any pointers to this admittedly open ended question?
Edit: code request (in Silverlight)- The following is a -snippet- from a mousebuttonup handler in a drag-drop allocation application that's part of a much larger app-
I just really don't like how blunt the logic is, and hate the way that it's all completely unfactorable, since everything is getting stuffed into event handlers.
//determine if there is a previously existing allocated sale corresponding to this purchase's ID
SaleWS allocSaleExisting = colltoaddsale.FirstOrDefault(s => (s.p_TRADEID == allocPurch.TRADEID));
if (allocSaleExisting != null && allocSale.TRADEID == allocSaleExisting.TRADEID)
{
PurchaseWS allocPurchExisting = colltoadd.First(p => p.TRADEID == allocPurch.TRADEID);
//allocPurchExisting.AMOUNT += allocPurch.AMOUNT;
allocSaleExisting.AMOUNT += allocSale.AMOUNT;
allocPurchExisting.AMOUNT += allocSale.AMOUNT;
allocPurch.AMOUNT -= allocSale.AMOUNT;
colltoaddsale.Remove(allocSale);
//colltoadd.Remove(allocPurch);
}
else
{
//Create new "split" item in the data source for the source table
PurchaseWS splitAllocPurch = new PurchaseWS { COMMODITY = allocPurch.COMMODITY, CONTRACTNUMBER = allocPurch.CONTRACTNUMBER, AMOUNT = allocPurch.AMOUNT - allocSale.AMOUNT, FORM = allocPurch.FORM, GRADE = allocPurch.GRADE, LOCATION = allocPurch.LOCATION, SHIP_DATE = allocPurch.SHIP_DATE, TRADEID = allocPurch.TRADEID, UNITS = allocPurch.UNITS };
//update the source table's selecteditem datacontext with the target allocation id
allocPurch.s_TRADEID = allocSale.TRADEID;
allocSale.p_TRADEID = allocPurch.TRADEID;
allocPurch.AMOUNT = allocSale.AMOUNT;
colltoadd.Insert(colltoadd.IndexOf(allocPurch) + 1, splitAllocPurch);
}
}
Take a look at the Composite Application Guidance from the Patterns and Practices group.
It's geared specifically towards this, including using MVVM for WPF/Silverlight in large scale applications, and how to handle business logic concerns, etc.
You should also check Caliburn.

Resources