UI: update chat view UI

Now view used for sending and receiving messages during the call (ChatView)
is the same as for regular ConversationView.
Also this commit fix two problems:
1)remove black imprints of text views, that appeared after window resizing
2)set status "read" for messages arriving during call, so they don't appear
in SmartList as unread.

Change-Id: I6d0cb79878395d28cfc93491a9d4cab42ed89192
Reviewed-by: Anthony Léonard <anthony.leonard@savoirfairelinux.com>
diff --git a/src/ConversationVC.mm b/src/ConversationVC.mm
index fb3d996..5739e0f 100644
--- a/src/ConversationVC.mm
+++ b/src/ConversationVC.mm
@@ -43,17 +43,17 @@
 #import "PhoneDirectoryModel.h"
 #import "account.h"
 #import "AvailableAccountModel.h"
+#import "MessagesVC.h"
 
 
 #import <QuartzCore/QuartzCore.h>
 
-@interface ConversationVC () <NSOutlineViewDelegate> {
+@interface ConversationVC () <NSOutlineViewDelegate, MessagesVCDelegate> {
 
     __unsafe_unretained IBOutlet NSTextField* messageField;
     QVector<ContactMethod*> contactMethods;
     NSMutableString* textSelection;
 
-    QNSTreeController* treeController;
     QMetaObject::Connection contactMethodChanged;
     ContactMethod* selectedContactMethod;
     SendContactRequestWC* sendRequestWC;
@@ -62,11 +62,10 @@
     __unsafe_unretained IBOutlet NSTextField* conversationTitle;
     __unsafe_unretained IBOutlet NSTextField* emptyConversationPlaceHolder;
     __unsafe_unretained IBOutlet IconButton* sendButton;
-    __unsafe_unretained IBOutlet NSOutlineView* conversationView;
     __unsafe_unretained IBOutlet NSPopUpButton* contactMethodsPopupButton;
+    IBOutlet MessagesVC* messagesViewVC;
 }
 
-@property (nonatomic, strong, readonly) INDSequentialTextSelectionManager* selectionManager;
 
 @end
 
@@ -82,7 +81,6 @@
 
     [sendPanel setWantsLayer:YES];
     [sendPanel setLayer:[CALayer layer]];
-    _selectionManager = [[INDSequentialTextSelectionManager alloc] init];
 
     [self setupChat];
 
@@ -106,8 +104,6 @@
                              return ;
                          }
 
-                         [self.selectionManager unregisterAllTextViews];
-
                          [contactMethodsPopupButton removeAllItems];
                          for (auto cm : contactMethods) {
                              [contactMethodsPopupButton addItemWithTitle:cm->uri().toNSString()];
@@ -145,8 +141,8 @@
 }
 
 - (IBAction)backPressed:(id)sender {
-    [conversationView setDelegate:nil];
     RecentModel::instance().selectionModel()->clearCurrentIndex();
+    messagesViewVC.delegate = nil;
 }
 
 - (IBAction)openSendContactRequestWindow:(id)sender
@@ -199,158 +195,6 @@
     [CATransaction commit];
 }
 
-#pragma mark - NSOutlineViewDelegate methods
-
-- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item;
-{
-    return YES;
-}
-
-- (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
-{
-    return YES;
-}
-
-- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
-{
-    QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
-    auto dir = qvariant_cast<Media::Media::Direction>(qIdx.data((int)Media::TextRecording::Role::Direction));
-    IMTableCellView* result;
-
-    if (dir == Media::Media::Direction::IN) {
-        result = [outlineView makeViewWithIdentifier:@"LeftMessageView" owner:self];
-    } else {
-        result = [outlineView makeViewWithIdentifier:@"RightMessageView" owner:self];
-    }
-
-    [result setup];
-
-    NSMutableAttributedString* msgAttString =
-    [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n",qIdx.data((int)Qt::DisplayRole).toString().toNSString()]
-                                           attributes:[self messageAttributesFor:qIdx]];
-
-    NSAttributedString* timestampAttrString =
-    [[NSAttributedString alloc] initWithString:qIdx.data((int)Media::TextRecording::Role::FormattedDate).toString().toNSString()
-                                    attributes:[self timestampAttributesFor:qIdx]];
-
-
-    CGFloat finalWidth = MAX(msgAttString.size.width, timestampAttrString.size.width);
-    finalWidth = MIN(finalWidth + 30, result.frame.size.width - result.photoView.frame.size.width - 30);
-
-    [msgAttString appendAttributedString:timestampAttrString];
-    [[result.msgView textStorage] appendAttributedString:msgAttString];
-    [result.msgView checkTextInDocument:nil];
-    [result.msgView setWantsLayer:YES];
-    result.msgView.layer.cornerRadius = 5.0f;
-
-    [result updateWidthConstraint:finalWidth];
-    [result.photoView setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(qIdx.data(Qt::DecorationRole)))];
-    return result;
-}
-
-- (void)outlineView:(NSOutlineView *)outlineView didAddRowView:(NSTableRowView *)rowView forRow:(NSInteger)row
-{
-    if (IMTableCellView* cellView = [outlineView viewAtColumn:0 row:row makeIfNecessary:NO]) {
-        [self.selectionManager registerTextView:cellView.msgView withUniqueIdentifier:@(row).stringValue];
-    }
-
-    if (auto txtRecording = contactMethods.at([contactMethodsPopupButton indexOfSelectedItem])->textRecording()) {
-        [emptyConversationPlaceHolder setHidden:txtRecording->instantMessagingModel()->rowCount() > 0];
-        txtRecording->setAllRead();
-    }
-}
-
-- (CGFloat)outlineView:(NSOutlineView *)outlineView heightOfRowByItem:(id)item
-{
-    QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
-
-    double someWidth = outlineView.frame.size.width;
-
-    NSMutableAttributedString* msgAttString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n",qIdx.data((int)Qt::DisplayRole).toString().toNSString()]
-                                                                                     attributes:[self messageAttributesFor:qIdx]];
-    NSAttributedString *timestampAttrString = [[NSAttributedString alloc] initWithString:
-                                               qIdx.data((int)Media::TextRecording::Role::FormattedDate).toString().toNSString()
-                                                                              attributes:[self timestampAttributesFor:qIdx]];
-
-    [msgAttString appendAttributedString:timestampAttrString];
-
-    NSRect frame = NSMakeRect(0, 0, someWidth, MAXFLOAT);
-    NSTextView *tv = [[NSTextView alloc] initWithFrame:frame];
-    [tv setEnabledTextCheckingTypes:NSTextCheckingTypeLink];
-    [tv setAutomaticLinkDetectionEnabled:YES];
-    [[tv textStorage] setAttributedString:msgAttString];
-    [tv sizeToFit];
-
-    double height = tv.frame.size.height + 20;
-
-    return MAX(height, 60.0f);
-}
-
-#pragma mark - Text formatting
-
-- (NSMutableDictionary*) timestampAttributesFor:(QModelIndex) qIdx
-{
-    auto dir = qvariant_cast<Media::Media::Direction>(qIdx.data((int)Media::TextRecording::Role::Direction));
-    NSMutableDictionary* attrs = [NSMutableDictionary dictionary];
-
-    if (dir == Media::Media::Direction::IN) {
-        attrs[NSForegroundColorAttributeName] = [NSColor grayColor];
-    } else {
-        attrs[NSForegroundColorAttributeName] = [NSColor whiteColor];
-    }
-
-    NSFont* systemFont = [NSFont systemFontOfSize:12.0f];
-    attrs[NSFontAttributeName] = systemFont;
-    attrs[NSParagraphStyleAttributeName] = [self paragraphStyle];
-
-    return attrs;
-}
-
-- (NSMutableDictionary*) messageAttributesFor:(QModelIndex) qIdx
-{
-    auto dir = qvariant_cast<Media::Media::Direction>(qIdx.data((int)Media::TextRecording::Role::Direction));
-    NSMutableDictionary* attrs = [NSMutableDictionary dictionary];
-
-    if (dir == Media::Media::Direction::IN) {
-        attrs[NSForegroundColorAttributeName] = [NSColor blackColor];
-    } else {
-        attrs[NSForegroundColorAttributeName] = [NSColor whiteColor];
-    }
-
-    NSFont* systemFont = [NSFont systemFontOfSize:14.0f];
-    attrs[NSFontAttributeName] = systemFont;
-    attrs[NSParagraphStyleAttributeName] = [self paragraphStyle];
-
-    return attrs;
-}
-
-- (NSParagraphStyle*) paragraphStyle
-{
-    /*
-     The only way to instantiate an NSMutableParagraphStyle is to mutably copy an
-     NSParagraphStyle. And since we don't have an existing NSParagraphStyle available
-     to copy, we use the default one.
-
-     The default values supplied by the default NSParagraphStyle are:
-     Alignment   NSNaturalTextAlignment
-     Tab stops   12 left-aligned tabs, spaced by 28.0 points
-     Line break mode   NSLineBreakByWordWrapping
-     All others   0.0
-     */
-    NSMutableParagraphStyle* aMutableParagraphStyle =
-    [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
-
-    // Now adjust our NSMutableParagraphStyle formatting to be whatever we want.
-    // The numeric values below are in points (72 points per inch)
-    [aMutableParagraphStyle setAlignment:NSLeftTextAlignment];
-    [aMutableParagraphStyle setLineSpacing:1.5];
-    [aMutableParagraphStyle setParagraphSpacing:5.0];
-    [aMutableParagraphStyle setHeadIndent:5.0];
-    [aMutableParagraphStyle setTailIndent:-5.0];
-    [aMutableParagraphStyle setFirstLineHeadIndent:5.0];
-    [aMutableParagraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
-    return aMutableParagraphStyle;
-}
 
 #pragma mark - NSTextFieldDelegate
 
@@ -378,17 +222,19 @@
                                             });
 
     if (auto txtRecording = selectedContactMethod->textRecording()) {
-        treeController = [[QNSTreeController alloc] initWithQModel:txtRecording->instantMessagingModel()];
-        [treeController setAvoidsEmptySelection:NO];
-        [treeController setChildrenKeyPath:@"children"];
-        [conversationView setDelegate:self];
-        [conversationView bind:@"content" toObject:treeController withKeyPath:@"arrangedObjects" options:nil];
-        [conversationView bind:@"sortDescriptors" toObject:treeController withKeyPath:@"sortDescriptors" options:nil];
-        [conversationView bind:@"selectionIndexPaths" toObject:treeController withKeyPath:@"selectionIndexPaths" options:nil];
+        messagesViewVC.delegate = self;
+        [messagesViewVC setUpViewWithModel:txtRecording->instantMessagingModel()];
     }
-
-    [conversationView scrollToEndOfDocument:nil];
 }
 
+#pragma mark - MessagesVC delegate
+
+-(void) newMessageAdded {
+
+    if (auto txtRecording = contactMethods.at([contactMethodsPopupButton indexOfSelectedItem])->textRecording()) {
+        [emptyConversationPlaceHolder setHidden:txtRecording->instantMessagingModel()->rowCount() > 0];
+        txtRecording->setAllRead();
+    }
+}
 
 @end